Loop through UCI array

We can access anonymous sections through an array, for example:

uci show network.@switch[0]
uci show fstab.@mount[-1]

How do I loop through these arrays? At least how can I get the array size?

This literally works but it is stupid:

index=0;
uci show fstab.@mount[$index] > /dev/null 2>&1;
while [ $? -eq 0 ]
do
    index=$((index + 1));
    # Your code goes here.
fi
2 Likes

I'm doing

i=0
while uci get bla.@user[$i] &> /dev/null ; do
	i=$((i+1));
done

1 Like

But an upstream solution would be nice.

. /lib/functions.sh

Ah, you are right! But not sure if I can remove something again. For example I have this hotplug.d script:

Nice functions.sh but i cant get it to echo the configuration.


#!/bin/sh
. /lib/functions.sh
config_get network.@wireguard_wggs[*].allowed_ips

does nothing.
I am sure i dont understand something here .
Can someone give me a hint, again ?
I need to iterate through all ips to find an unused one.
I could use :

i=0
while uci get bla.@user[$i] &> /dev/null ; do
	i=$((i+1));
done

but i like to learn it the openwrt style right away. I am sure if i unsderstand the logic behind the functions.sh i made a big step to program better shell scripts for openwrt.

Thank you guys.

THIS LOOKS PROMISSING:

Have you read this: https://openwrt.org/docs/guide-developer/config-scripting ?

1 Like

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.