Some openwrt based forks come without the uci
utility. I don't want to build uci
from its c source and uci
isn't available in the entware-ng-3x repo.
A tab delimited uci configuration file looks like:
# Whitelist regex strings examples
# list whitelist '^10\.0\.[01]\..*$'
# list whitelist '^192\.168\.1\..*$'
# RBL URLs - some (but not all) will also support http
list rbl 'https://sigs.interserver.net/iprbl.txt'
list rbl 'https://rbldata.interserver.net/ip.txt'
list rbl 'https://rbldata.interserver.net/ipslim.txt'
With the help of idiomatic-awk blog post and https://unix.stackexchange.com/a/286794/17560, I was able to parse the uci .conf file using awk
(busybox 1.24+):
$ awk '!(/^\t*#/) && /\trbl\t/ {print $3}' config/file.conf
'https://sigs.interserver.net/iprbl.txt'
'https://rbldata.interserver.net/ip.txt'
'https://rbldata.interserver.net/ipslim.txt'
A possible improvement, to catch editor the indent with spaces instead of tabs, is to directly match the second column for its value without the tab characters, like this:
awk '!(/^\t*#/) && $2 == "rbl" {print $3}'
How can this uci configuration file parsing be further improved?
PS Keep in mind that the platform is a router with busybox (without gnu utils), [:blank:]
to match spaces and tabs seems not understood by busybox awk
, entware-ng-3x package installation is ok.
The structure of a UCI file makes it possible to parse it with the shell alone (including busybox shell). Some scripts just do this in OpenWRT.
You first define three shell functions named config(), option(), list()
Then you source the UCI file. It will take the config, option, list lines as if they were shell commands with 1 or 2 arguments.
e.g. UCI file "file.conf":
config myconfigtype 'myconfiginstance'
option myoption 'myvalue'
option otheroption 'other value'
shell script "uci.inc":
config() {
echo I got a config section type $1 instance $2
CONF="$2"
}
option() {
echo I got an option named $1 value $2 pertaining to config $CONF
}
Main shell script:
#!/bin/sh
. uci.inc # read shell library
. file.conf # parse file.conf