Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
docs:guide-developer:config-scripting [2021/07/24 05:39] – [Configuration in scripts] someothertimedocs:guide-developer:config-scripting [2024/06/01 08:50] (current) – code bash highlighting stokito
Line 10: Line 10:
 To be able to load UCI configuration files, you need to include the common functions with: To be able to load UCI configuration files, you need to include the common functions with:
  
-<code>+<code bash>
 . /lib/functions.sh . /lib/functions.sh
 </code> </code>
Line 16: Line 16:
 (If your startup script starts with: (If your startup script starts with:
  
-<code>+<code bash>
 #!/bin/sh /etc/rc.common #!/bin/sh /etc/rc.common
 </code> </code>
Line 43: Line 43:
   - Name, the section name, e.g. ''wan'' for ''config interface wan'' or an autogenerated ID like ''cfg13abef'' for anonymous sections like ''config route''   - Name, the section name, e.g. ''wan'' for ''config interface wan'' or an autogenerated ID like ''cfg13abef'' for anonymous sections like ''config route''
  
-<code>+<code bash>
 config_cb() { config_cb() {
     local type="$1"     local type="$1"
Line 62: Line 62:
   - Value, the option value, e.g. ''eth0'' for ''option ifname eth0''   - Value, the option value, e.g. ''eth0'' for ''option ifname eth0''
  
-<code>+<code bash>
 option_cb() { option_cb() {
     local name="$1"     local name="$1"
Line 83: Line 83:
   - Value, the list value, e.g. ''192.168.42.1'' for ''list server 192.168.42.1''   - Value, the list value, e.g. ''192.168.42.1'' for ''list server 192.168.42.1''
  
-<code>+<code bash>
 list_cb() { list_cb() {
     local name="$1"     local name="$1"
Line 106: Line 106:
 in ''/etc/config/network''. The string ''test'' is passed as second argument on each invocation. in ''/etc/config/network''. The string ''test'' is passed as second argument on each invocation.
  
-<code>+<code bash>
 handle_interface() { handle_interface() {
     local config="$1"     local config="$1"
Line 117: Line 117:
 </code> </code>
  
-It is possible to abort the iteration from within the callback by returning a non-zero value (''return 1'').+Note that ''config_foreach'' will iterate through all sections without regard to the callback function'return value.
  
 Within the per-section callback, the ''config_get'' or ''config_set'' procedures may be used to read or set Within the per-section callback, the ''config_get'' or ''config_set'' procedures may be used to read or set
Line 130: Line 130:
   - Default (optional), value to return instead if option is unset   - Default (optional), value to return instead if option is unset
  
-<code> +<code bash
-...+#...
 # read the value of "option ifname" into the "iface" variable # read the value of "option ifname" into the "iface" variable
 # $config contains the ID of the current section # $config contains the ID of the current section
Line 137: Line 137:
 config_get iface "$config" ifname config_get iface "$config" ifname
 echo "Interface name is $iface" echo "Interface name is $iface"
-...+#...
 </code> </code>
  
Line 148: Line 148:
   - Value to assign   - Value to assign
  
-<code>+<code bash>
 ... ...
 # set the value of "option auto" to "0" # set the value of "option auto" to "0"
Line 167: Line 167:
 The example below reads "option proto" from the "config interface wan" section. The example below reads "option proto" from the "config interface wan" section.
  
-<code>+<code bash>
 ... ...
 local proto local proto
Line 210: Line 210:
   - Additional arguments (optional), all following arguments are passed to the callback procedure as-is   - Additional arguments (optional), all following arguments are passed to the callback procedure as-is
  
-<code>+<code bash>
 # handle list items in a callback # handle list items in a callback
 # $config contains the ID of the section # $config contains the ID of the section
Line 247: Line 247:
 For instance, consider this script: For instance, consider this script:
  
-<code>+<code bash>
 config_cb() { config_cb() {
     local type="$1"     local type="$1"
Line 319: Line 319:
 That is, the type should come first, then selectors (with a special case for ''local'', which doesn't take an argument), and finally the action.  The basic parsing method is the following: That is, the type should come first, then selectors (with a special case for ''local'', which doesn't take an argument), and finally the action.  The basic parsing method is the following:
  
-<code>+<code bash>
 parse_filter() { parse_filter() {
     local section="$1"     local section="$1"
  • Last modified: 2021/07/24 05:39
  • by someothertime