我需要从lua脚本中的.conf文件加载配置变量,并使用这些变量连接到数据库。我尝试过使用:
require "host.conf"
loadfile("host.conf") - error with unexpected token '#'
os.execute("pathToConfFile/host.lua") - and I have created a lua host file with variables in bash shell
io.popen("host.conf") etc..
这些解决方案都不是有效的。有没有办法在lua中使用现有的host.conf文件,并避免意外的令牌错误?谢谢你的建议。
发布于 2021-01-19 19:28:29
感谢每一个帮助过我们的人。我的问题不准确,在我问之前我有更多的东西要学,对此我深表歉意。这就是我用来解决问题的方法。
local open = io.open
local function read_file(path)
local file = open(path, "r")
if not file then return nil end
local content = file:read "*a" -- *a or *all reads the whole file
local lines = {}
for line in io.lines(path) do
--print(line);
if(line:find(var)~=nil)then
local varStart=string.len(var)+2
local varEnd=string.len(line)
var=string.sub(line,varStart,varEnd)
print(var);
end
--repeat for every line
end
file:close()
return lines;
end
local fileContent = read_file("path");
https://stackoverflow.com/questions/65690831
复制相似问题