Differences
This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision Next revision | Previous revision | ||
| docs:guide-developer:config-scripting [2018/03/03 20:23] – ↷ Links adapted because of a move operation | docs:guide-developer:config-scripting [2024/06/01 08:50] (current) – code bash highlighting stokito | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| ====== Configuration in scripts ====== | ====== Configuration in scripts ====== | ||
| - | OpenWrt offers a set of standard shell procedures to interface with UCI in order to efficiently read | + | {{section> |
| + | |||
| + | OpenWrt offers a set of standard shell procedures to interface with [[docs: | ||
| and process configuration files from within shell scripts. | and process configuration files from within shell scripts. | ||
| 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 bash> |
| + | . / | ||
| + | </ | ||
| + | |||
| + | (If your startup script starts with: | ||
| + | |||
| + | <code bash> | ||
| + | #!/bin/sh / | ||
| + | </ | ||
| + | |||
| + | then you do not need to include | ||
| Then you can use '' | Then you can use '' | ||
| Line 31: | Line 43: | ||
| - Name, the section name, e.g. '' | - Name, the section name, e.g. '' | ||
| - | | '' | + | <code bash> |
| + | config_cb() { | ||
| local type=" | local type=" | ||
| local name=" | local name=" | ||
| # commands to be run for every section | # commands to be run for every section | ||
| - | }'' | + | } |
| + | </ | ||
| Also an extra call to '' | Also an extra call to '' | ||
| Line 48: | Line 62: | ||
| - Value, the option value, e.g. '' | - Value, the option value, e.g. '' | ||
| - | | '' | + | <code bash> |
| + | option_cb() { | ||
| local name=" | local name=" | ||
| local value=" | local value=" | ||
| # commands to be run for every option | # commands to be run for every option | ||
| - | }'' | + | } |
| + | </ | ||
| Within the callback, the ID of the current section is accessible via the '' | Within the callback, the ID of the current section is accessible via the '' | ||
| Line 67: | Line 83: | ||
| - Value, the list value, e.g. '' | - Value, the list value, e.g. '' | ||
| - | | '' | + | <code bash> |
| + | list_cb() { | ||
| local name=" | local name=" | ||
| local value=" | local value=" | ||
| # commands to be run for every list item | # commands to be run for every list item | ||
| - | }'' | + | } |
| + | </ | ||
| Each item of a given list generates a new call to '' | Each item of a given list generates a new call to '' | ||
| Line 88: | Line 106: | ||
| in ''/ | in ''/ | ||
| - | | '' | + | <code bash> |
| + | handle_interface() { | ||
| local config=" | local config=" | ||
| local custom=" | local custom=" | ||
| Line 95: | Line 114: | ||
| config_load network | config_load network | ||
| - | config_foreach handle_interface interface test'' | + | config_foreach handle_interface interface test |
| + | </ | ||
| - | It is possible | + | Note that '' |
| Within the per-section callback, the '' | Within the per-section callback, the '' | ||
| Line 110: | Line 130: | ||
| - Default (optional), value to return instead if option is unset | - Default (optional), value to return instead if option is unset | ||
| - | | '' | + | <code bash> |
| - | # read the value of " | + | #... |
| - | # $config contains the ID of the current section | + | # read the value of " |
| - | local iface | + | # $config contains the ID of the current section |
| - | config_get iface " | + | local iface |
| - | echo " | + | config_get iface " |
| - | ...'' | + | echo " |
| + | #... | ||
| + | </ | ||
| Line 126: | Line 148: | ||
| - Value to assign | - Value to assign | ||
| - | | '' | + | <code bash> |
| - | # set the value of " | + | ... |
| - | # $config contains the ID of the current section | + | # set the value of " |
| - | config_set " | + | # $config contains the ID of the current section |
| - | ...'' | + | config_set " |
| - | + | ... | |
| - | Note that values changed with '' | + | </code> |
| - | return the updated values but the underlying configuration files are //not// altered. If you want to alter values, use the uci_* functions from / | + | |
| + | Note: | ||
| + | * values changed with '' | ||
| + | * config_set is arcane and buggy (config_get is fine), use "uci set" instead. | ||
| ===== Direct access ===== | ===== Direct access ===== | ||
| Line 143: | Line 167: | ||
| The example below reads " | The example below reads " | ||
| - | | '' | + | <code bash> |
| + | ... | ||
| local proto | local proto | ||
| config_get proto wan proto | config_get proto wan proto | ||
| echo " | echo " | ||
| - | ...'' | + | ... |
| + | </ | ||
| Line 154: | Line 180: | ||
| Some UCI configurations may contain '' | Some UCI configurations may contain '' | ||
| - | | '' | + | < |
| + | ... | ||
| list network lan | list network lan | ||
| list network wifi | list network wifi | ||
| - | ...'' | + | ... |
| + | </ | ||
| Calling '' | Calling '' | ||
| Line 163: | Line 191: | ||
| However, this behaviour might break values if the list items itself contain spaces like illustrated below: | However, this behaviour might break values if the list items itself contain spaces like illustrated below: | ||
| - | | '' | + | < |
| + | ... | ||
| list animal 'White Elephant' | list animal 'White Elephant' | ||
| list animal ' | list animal ' | ||
| - | ...'' | + | ... |
| + | </ | ||
| The '' | The '' | ||
| Line 180: | 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 bash> |
| + | # handle list items in a callback | ||
| # $config contains the ID of the section | # $config contains the ID of the section | ||
| handle_animal() { | handle_animal() { | ||
| Line 187: | Line 218: | ||
| } | } | ||
| - | config_list_foreach " | + | config_list_foreach " |
| + | </ | ||
| Note: for editing value, use the [[docs: | Note: for editing value, use the [[docs: | ||
| Line 215: | Line 247: | ||
| For instance, consider this script: | For instance, consider this script: | ||
| - | < | + | < |
| config_cb() { | config_cb() { | ||
| local type=" | local type=" | ||
| Line 234: | Line 266: | ||
| This would parse a configuration like this one: | This would parse a configuration like this one: | ||
| - | | '' | + | < |
| + | config mysection foo | ||
| option link_quality auto | option link_quality auto | ||
| option rxcost 256 | option rxcost 256 | ||
| - | option hello_inverval 4'' | + | option hello_inverval 4 |
| + | </ | ||
| And generate a file containing: | And generate a file containing: | ||
| - | | '' | + | < |
| + | link-quality auto | ||
| rxcost 256 | rxcost 256 | ||
| - | hello-interval 4'' | + | hello-interval 4 |
| + | </ | ||
| ==== Use case and example for direct access ==== | ==== Use case and example for direct access ==== | ||
| Line 283: | Line 319: | ||
| That is, the type should come first, then selectors (with a special case for '' | That is, the type should come first, then selectors (with a special case for '' | ||
| - | < | + | < |
| parse_filter() { | parse_filter() { | ||
| local section=" | local section=" | ||