[UCI] List of unnamed sections

Hi,

I want to use multiple unnamed sections and iterate through them. According to the wiki I set up multiple unnamed sections.

config anchorID 
	option ip '10.12.1.112'
	option feather_id '2'

config anchorID 
	option ip '10.12.1.113'
	option feather_id '3'

config anchorID 
	option ip '10.12.1.114'
	option feather_id '4'

config anchorID 
	option ip '10.12.1.115'
	option feather_id '5'

Now I do see them if I show the whole config:

:~# uci show avarange-daemon
avarange-daemon.@anchorID[0]=anchorID
avarange-daemon.@anchorID[0].ip='10.12.1.112'
avarange-daemon.@anchorID[0].feather_id='2'
avarange-daemon.@anchorID[1]=anchorID
avarange-daemon.@anchorID[1].ip='10.12.1.113'
avarange-daemon.@anchorID[1].feather_id='3'
avarange-daemon.@anchorID[2]=anchorID
avarange-daemon.@anchorID[2].ip='10.12.1.114'
avarange-daemon.@anchorID[2].feather_id='4'
avarange-daemon.@anchorID[3]=anchorID
avarange-daemon.@anchorID[3].ip='10.12.1.115'
avarange-daemon.@anchorID[3].feather_id='5'

But for accessing them in my script it would be nice to know the "length" of this kind of array.

I tried things like

:~# uci get avarange-daemon.@anchorID[@]
uci: Invalid argument
:~# uci get avarange-daemon.@anchorID
uci: Invalid argument
:~# uci get avarange-daemon.anchorID

or like this

:~# uci show avarange-daemon.anchorID
uci: Entry not found
:~# uci show avarange-daemon.@anchorID
uci: Invalid argument

In this end, I do know ways with python and the pyuci do work around it, but it feels like that this should be a feature of uci.

Did I miss something in the wiki article ?
Or is there already a way to get all "unamed section with the same type" ?

1 Like

Use https://openwrt.org/docs/guide-developer/config-scripting#iterating_config_foreach

. /lib/functions.sh

handle_anchor() {
    local sid=$1
    local ip id

    config_get ip "$sid" ip
    config_get id "$sid" feather_id

    echo "IP: $ip / ID: $id"
}

config_load avarange-daemon
config_foreach handle_anchor anchorID
1 Like