Lua调用uci命令及linux命令

Lua调用uci命令及linux命令,第1张

最近项目接触到Openwrt的编译和使用,op本身是一个定制的linux系统,兼容的包和语言也有很多,随着物联网的发展,相信在路由器方面的应用会越来越多,luci作为一个已经在openwrt上集成的web管理工具有很强大的功能,但我的项目里面需要修改和使用自己的配置文件,因此整理一下lua+uci的使用。

1、首先,连接相应linux主机,进入到linux命令行状态下,等待输入shell指令。

2、执行./lua文件进入命令 *** 作模式后,按ESC,输入::q!。

3、键盘按“回车键”运行shell指令,此时会发现成功退出了文件的命令行 *** 作界面。

你可以参考如下实例代码:function getFile(file_name) 

    local f = assert(io.open(file_name, 'r'))

    local string = f:read("*all")

    f:close()

    return string

end function writeFile(file_name,string)

 local f = assert(io.open(file_name, 'w'))

 f:write(string)

 f:close()

end --从命令行获取参数, 如果有参数则遍历指定目录,没有参数遍历当前目录 if arg[1] ~= nil then

     cmd = "ls "..arg[1]

else

     cmd = "ls" end print("cmd", cmd)

--io.popen 返回的是一个FILE,跟c里面的popen一样 local s = io.popen(cmd)

local fileLists = s:read("*all")

print(fileLists)

while true do --从文件列表里一行一行的获取文件名 _,end_pos, line = string.find(fileLists, "([^\n\r]+.txt)", start_pos)

        if not end_pos then break end --    print ("wld", line) local str = getFile(line)

    --把每一行的末尾 1, 替换为 0, local new =string.gsub(str, "1,\n", "0,\n")

    --替换后的字符串写入到文件。以前的内容会清空     writeFile(line, new)

    start_pos = end_pos + 1 end


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/8923262.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-23
下一篇 2023-04-23

发表评论

登录后才能评论

评论列表(0条)

保存