I use UCI together with bash
. In case of other shells the variable and loop syntax might have to be adjusted accordingly.
-
Reference the newly added item with
[-1]
:uci add firewall rule uci set firewall.@rule[-1].name=test
Each newly added entry will again be available under
[-1]
. -
Assign the new entry to a shell variable:
rule=$(uci add firewall rule) uci set firewall.$rule.src='lan' pbr_policy=$(uci add pbr policy) uci set pbr.$pbr_policy.name='test'
When saving to a variable, you capture the unique key of the new config entry. I prefer this method as it's more predictable in scripts and less error prone.
In case of a new config entry, where you modify just one property, you could combine two lines into one:uci set pbr.$(uci add pbr policy).name='test'
[0]
lets you hook to the first unnamed section in the config file:
config dnsmasq
option domain 'lan'
uci set dhcp.@dnsmasq[0].domain='home'
config adblock 'global'
option adb_fetchutil 'curl'
uci set adblock.global.adb_fetchutil='uclient-fetch'
While deleting, the indexes of the remaining config entries change right away without committing (uci commit
), e.g. entry 1 becomes entry 0 immediately once the previous 0-indexed entry gets removed!
Real world example: you install the PBR package, and wanna delete two default PBR policies from the config file provided by the package.
You would most likely use this loop:
for i in {0..1}; do uci -q delete pbr.@policy[$i]; done
but it won't work - it would remove just the first policy!
The correct approach is:
for i in {0..1}; do uci -q delete pbr.@policy[0]; done