Configuration in scripts
OpenWrt offers a set of standard shell procedures to interface with UCI in order to efficiently read and process configuration files from within shell scripts.
Loading
To be able to load UCI configuration files, you need to include the common functions with:
. /etc/functions.sh |
Then you can use config_load name to load config files.
The function first checks for name as absolute filename and falls back to loading it from
/etc/config/ (which is the most common way of using it).
If you want to use special callbacks for sections and/or options, you need to redefine the shell
functions explained below before running config_load, but after including /etc/functions.sh.
Callbacks
The config_cb procedure is called every time a UCI section heading is encountered during parsing.
When called, the procedure receives two arguments:
- Type, the section type, e.g.
interfaceforconfig interface wan - Name, the section name, e.g.
wanforconfig interface wanor an autogenerated ID likecfg13abeffor anonymous sections likeconfig route
config_cb() {
local type="$1"
local name="$2"
# commands to be run for every section
} |
Similar to config_cb, the option_cb procedure is called each time a UCI option (or list) is encountered.
When called, the procedure receives two arguments:
- Name, the option name, e.g.
ifnameforoption ifname eth0 - Value, the option value, e.g.
eth0foroption ifname eth0
option_cb() {
local name="$1"
local value="$2"
# commands to be run for every option
} |
You can also alter option_cb from config_cb based on the section type.
This allows you to process every single config section based on its type individually.
Within the callbacks, the ID of the current section is accessible via the CONFIG_SECTION variable.
Also an extra call to config_cb (without a new section) is generated after config_load is done -
this allows you to process sections both before and after all options were processed.
Iterating
An alternative approach to callback based parsing is iterating the configuration sections
with the config_foreach procedure.
The config_foreach procedure takes at least one argument:
- Function, name of a previously defined procedure called for each encountered section
- Type (optional), only iterate sections of the given type, skip others
- Additional arguments (optional), all following arguments are passed to the callback procedure as-is
In the example below, the handle_interface procedure is called for each config interface section
in /etc/config/network. The string test is passed as second argument on each invocation.
handle_interface() {
local config="$1"
local custom="$2"
# run commands for every interface section
}
config_load network
config_foreach handle_interface interface test |
It is possible to abort the iteration from within the callback by returning a non-zero value (return 1).
Within the per-section callback, the config_get or config_set procedures may be used to read or set
values belonging to the currently processed section.
Reading options
The config_get procedure takes at least three arguments:
- Name of a variable to store the retrieved value in
- ID of the section to read the value from
- Name of the option to read the value from
- Default (optional), value to return instead if option is unset
…
# read the value of "option ifname" into the "iface" variable
# $config contains the ID of the current section
local iface
config_get iface "$config" ifname
echo "Interface name is $iface"
… |
Setting options
The config_set procedure takes three arguments:
- ID of the section to set the option in
- Name of the option to assign the value to
- Value to assign
…
# set the value of "option auto" to "0"
# $config contains the ID of the current section
config_set "$config" auto 0
… |
Note that values changed with config_set are only kept in memory. Subsequent calls to config_get will
return the updated values but the underlying configuration files are not altered. If you want to alter values, use the uci_* functions from /lib/config/uci.sh which are automatically included by /etc/functions.sh.
Direct access
If the name of a configuration section is known in advance (it is named), options can be read directly without using a section iterator callback.
The example below reads "option proto" from the "config interface wan" section.
…
local proto
config_get proto wan proto
echo "Current WAN protocol is $proto"
… |
Reading lists
Some UCI configurations may contain list options in the form:
…
list network lan
list network wifi
… |
Calling config_get on the network list will return the list values separated by space, lan wifi in this example.
However, this behaviour might break values if the list items itself contain spaces like illustrated below:
…
list animal 'White Elephant'
list animal 'Mighty Unicorn'
… |
The config_get approach would return the values in the form White Elephant Mighty Unicorn and the original list items
are not clearly separated anymore.
To circumvent this problem, the config_list_foreach iterator can be used. It works similar to config_foreach but
operates on list values instead of config sections.
The config_list_foreach procedure takes at least three arguments:
- ID of the section to read the list from
- Name of the list option to read items from
- Procedure to call for each list item
- Additional arguments (optional), all following arguments are passed to the callback procedure as-is
# handle list items in a callback
# $config contains the ID of the section
handle_animal() {
local value="$1"
# do something with $value
}
config_list_foreach "$config" animal handle_animal |
Reading booleans
Boolean options may contain various values to signify a true value like on, true, enabled or 1.
The config_get_bool procedure simplifies the process of reading a boolean option and casting it to a plain
integer value (1 for true and 0 for false).
At least three arguments are expected by the procedure:
- Name of a variable to store the retrieved value in
- ID of the section to read the value from
- Name of the option to read the value from
- Default (optional), boolean value to return if option is unset
doc/devel/config-scripting.txt · Last modified: 2011/05/25 12:02 by soma