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 [2019/01/12 19:38] – [Loading] add note about /etc/rc.common jefferytodocs:guide-developer:config-scripting [2024/06/01 08:50] (current) – code bash highlighting stokito
Line 1: Line 1:
 ====== Configuration in scripts ====== ====== Configuration in scripts ======
 +
 +{{section>meta:infobox:howto_links#config-network-device&noheader&nofooter&noeditbutton}}
  
 OpenWrt offers a set of standard shell procedures to interface with [[docs:guide-user:base-system:uci|UCI]] in order to efficiently read OpenWrt offers a set of standard shell procedures to interface with [[docs:guide-user:base-system:uci|UCI]] in order to efficiently read
Line 8: 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 14: 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 41: 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 60: 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 81: 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 104: 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 115: 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 128: 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 135: 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 146: 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 165: 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 208: 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 245: 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 317: 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: 2019/01/12 19:38
  • by jefferyto