Go...

当前位置: 首页>>世界杯1998

UCI (统一配置接口) – 技术参考资料

花一点时间来理解 "section" 和 "type" 之间的差异是值得的. 让我们从一个例程开始:

#uci show system

system.@system[0]=system

system.@system[0].hostname=OpenWrt system.@system[0].timezone=UTC

system.@rdate[0]=rdate

system.@rdate[0].server=ac-ntp0.net.cmu.edu ptbtime1.ptb.de ac-ntp1.net.cmu.edu ntp.xs4all.nl ptbtime2.ptb.de cudns.cit.cornell.edu ptbtime3.ptb.de

这里, x:get("system","@rdate[0]","server") 将不正常工作. 因为 rdate 是个 type, 不是 section.

执行 x:get_all("system")的返回结果如下:

{ cfg02f02f={[".name"]="cfg02f02f",[".type"]="system",hostname="OpenWrt",[".index"]=0,[".anonymous"]=true,timezone="UTC"}, cfg04e10c={[".name"]="cfg04e10c",[".type"]="rdate",[".index"]=1,[".anonymous"]=true, server={"ac-ntp0.net.cmu.edu","ptbtime1.ptb.de","ac-tp1.net.cmu.edu","ntp.xs4all.nl","ptbtime2.ptb.de","cudns.cit.cornell.edu","ptbtime3.ptb.de"}} }

[".type"] 列出了段的类型

[".name"] 列出了段实际名称。这里可以看到,这些名称是系统生成的。

[".index"] 是列表的索引(+1)

据我所知,好像还没有办法直接访问"@rdate[0]"。你必须用x:foreach 迭代列出所给定类型的所有元素。

我一般用下面的函数:

uci=require("uci")

function getConfType(conf,type)

local curs=uci.cursor()

local ifce={}

curs:foreach(conf,type,function(s) ifce[s[".index"]]=s end)

return ifce

end

getConfType("system","rdate") 返回:

{{[".name"]="cfg04e10c",[".type"]="rdate",[".index"]=1,[".anonymous"]=true,

server={"ac-ntp0.net.cmu.edu","ptbtime1.ptb.de","ac-ntp1.net.cmu.edu","ntp.xs4all.nl","ptbtime2.ptb.de","cudns.cit.cornell.edu","ptbtime3.ptb.de"}}}

所以,如果你想修改system.@rdate[0].server,你需要迭代所需类型的段名([".name"]),然后调用x:set("system","cfg04e10c","server","zzz.com")

希望这对你有帮助

Sophana

(不过, Luci有一个Cursor:get_first 函数实现了类似的功能,

不同的是他类型而不是段作为第二个参数.)