Number of sections in a config file

Is it possible to know the number of sections in a config file under /etc/.

For example for the file /etc/config/application, the command "uci show application.@section[1]" returns all the options under section 1.

In similar way, using UCI, is there any command which returns number of sections in config file.

Now to check whether the execution has reached end of file or not, is to run by incrementing the sections number until it reaches "uci: Entry not found"

Not a particularly nice way of doing it, but:

uci show firewall | grep "firewall.@section.*=section" | wc -l

and subtract 1 from that number.

1 Like
n_sections=0
while uci -q get application.@section[$n_sections] >/dev/null; do
    n_sections=$((n_sections + 1))
done
2 Likes

yet another one ... without uci, pipes etc. ... :wink:

grep -c "^config" /etc/config/firewall
2 Likes

If my guess that you're trying to iterate over config file is correct, I'd highly recommend you consider using the config_cb and the like callback options described here.

1 Like