uci是openwrt上配置操作的接口,不管是自动化的shell脚本,还是使用luci来二次开发配置界面,都会用到这部分知识。uci提供了lua,shell,c接口,这里主要用到了前两种
shell接口
文档地址,增删改查都有,这里简单使用下。
下面的配置为例子
root@xCloud:~# cat /etc/config/test
config test 'abc'
option test_var2 'value22'
option test_var 'value11'
config tt1
option name 'lzz'
查看配置
root@xCloud:~# uci show test
test.abc=test
test.abc.test_var2='value22'
test.abc.test_var='value11'
test.@tt1[0]=tt1
test.@tt1[0].name='lzz'
显示配置的文本
root@xCloud:~# uci export test
package test
config test 'abc'
option test_var2 'value22'
option test_var 'good'
config tt1
option name 'lzz'
修改一个配置项的值
oot@xCloud:~# uci set test.abc.test_var='good'
root@xCloud:~# uci commit test
root@xCloud:~# uci show test.abc.test_var
test.abc.test_var='good'
获取一个配置项的值
root@xCloud:~# uci get test.abc.test_var
good
root@xCloud:~# uci show test.abc.test_var
test.abc.test_var='good'
root@xCloud:~# uci show test.@tt1[0].name
test.cfg03af7e.name='lzz'
root@xCloud:~# uci get test.@tt1[0].name
lzz
lua接口
通过下面的例子理解
#!/usr/bin/lua
print('Testing start..')
require('uci')
-- Get asection type or an option
x =uci.cursor()
a =x:get('test', 'abc', 'test_var')
if a == nil then
print('can't found the config file')
return
else
print(a)
end
x:set('test', 'abc', 'test_var', 'value11')
tt1 = x:add('test', 'tt1')
x:set('test', tt1, 'name', 'lzz')
-- Getthe configuration directory
b =x:get_confdir()
print(b)
-- Getall sections of a config or all values of a section
d = x:get_all('test', 'abc')
print('config test abc')
print(d)
print(d['test_var'])
print(d['test_var2'])
-- loop a config
x:foreach('test', 'tt1', function(s)
print('----')
for k,v in pairs(s) do
print(k..':'..tostring(v))
end
end)
-- discard changes, that have not been commited
-- x:revert('test')
-- commit changes
x:commit('test')
--[[
/etc/config/test
config 'test' 'abc'
option 'test_var' 'value'
option 'test_var2' 'value22'
]]
代码最后的注释是测试config的路径和内容
文章为作者独立观点,不代表 股票程序化软件自动交易接口观点