openwrt uci

UCI: Unified Configuration Interface 通用配置接口,主要用於集中控制openwrt的配置文件。web

一、uci使用的配置文件通常放置在設備上的/etc/config目錄下,包括一些兼容的應用,譬如snmp,保存在/etc/config/的配置文件是已經被重寫後的文件。數組

而且這些配置文件被寫在RAM,而不是flash中,由於它沒有必要被寫在不易失的內存中,而且他們會常常改變。app

存在於/etc/config下面的文件,能夠使用直接修改或者經過uci命令的方式,還有兼容的luci頁面等來進行修改。配置文件被修改後,要想使配置生效,必須重啓相應的進程。ide

 

二、uci配置文件的語法ui

uci配置文件包含了多個「config」段。this

package 'example'

config 'example' 'test'
        option   'string'      'some value'
        option   'boolean'     '1'
        list     'collection'  'first item'
        list     'collection'  'second item'
  • The  config 'example' 'test'  statement defines the start of a section with the type example and the name test. There can also be so called anonymous sections with only a type, but no name identifier. The type is important for the processing programs to decide how to treat the enclosed options.

        「example」和「test」定義了一個段,段的類型爲example,名字爲test。也有匿名段,即只有段類型(example),木有段名稱(test).spa

  • The  option 'string' 'some value'  and  option 'boolean' '1'  lines define simple values within the section. Note that there are no syntactical differences between text and boolean options. Per convention, boolean options may have one of the values '0', 'no', 'off', 'false' or 'disabled' to specify a false value or '1' , 'yes', 'on', 'true' or 'enabled' to specify a true value.

         「string」和「some value」組成了一對值(value)。而且對於文本和布爾型沒有明確的界限命令行

  • In the lines starting with a  list keyword an option with multiple values is defined. All list statements that share the same name, collection in our example, will be combined into a single list of values with the same order as in the configuration file.

        關鍵字「list」定義了一個數組,共用一個數組名(例如本例中的 collection),code

  • The indentation of the  option and list statements is a convention to improve the readability of the configuration file but it's not syntactically required.
  • If an option is absent and not required, the default value is assumed. If it is absent and required, it may trigger an error in the application or other unwanted behaviour.
  • A way to disable a config section, that does not have a  enabled option to be disabled, is renaming the config section identifier (or type, in this case example) to a value not recognized by the processes that uses those values. Normally a disabled_identifier as config section type/identifier is sufficient.

       一種禁止section段的方法,就是重命名section段名(或者是類型type名),總之就是使使用這個配置文件的進程識別不出來。orm

 

It is important to know that UCI identifiers and config file names may contain only the characters a-z, 0-9 and _. 

uci配置文件中使用的變量只支持a-z,0-9和下劃線_.

三、使用命令行修改配置文件

 對於匿名段,須要加@和取第一個[0]的符號纔可以正常獲取和修改值。

例如:

root@hbg:/# cat /etc/config/snmpd
config agent
        option agentaddress UDP:161

利用查看命令你會查看到:

root@hbg:/# uci show snmpd
snmpd.@agent[0]=agent
snmpd.@agent[0].agentaddress=UDP:161

 

所以你要查看agent下的值時須要使用命令:

root@hbg:/# uci get snmpd.@agent[0].agentaddress
UDP:161
root@TVWS:/#

不然會報錯:

root@hbg:/# uci get snmpd.agent.agentaddress
uci: Entry not found
root@TVWS:/

 

來源於:  http://wiki.openwrt.org/doc/uci

相關文章
相關標籤/搜索