--[[
判断文件是否存在
@param path 文件路径
@return true存在,false不存在
--]]
function file_exists(path)
local f=io.open(path,"r")
if f~=nil then io.close(f) return true else return false end
end
--[[
执行系统命令,不输出信息
@param cmd string
@return 系统状态码 0 成功 1 失败
]]--
function execute(cmd)
return os.execute(cmd)
end
--[[
执行系统命令,并返回执行结果字符串
@param cmd string
@return output string
]]--
function executeCmd(cmd)
local f = io.popen(cmd)
local a = f:read("*all")
f:close()
return a
end
--[[
去除字符串两边空格
Removes spaces from the begining and end of a given string
@param s string to be trimmed
@return trimmed string
]]--
local function trim (s)
if not s then
return nil
end
return (string.gsub(s, "^%s*(.-)%s*$", "%1"))
end
--[[
分隔字符串
调用方法 local list = split(",", "221.236.0.0,255.255.0.0,192.168.241.2")
返回值
for k, v in pairs(list) do
print(k,v)
end
1 221.236.0.0
2 255.255.0.0
3 192.168.241.2
]]--
function split(delim, text)
local list = {}
if type(text) ~= "string" then
return text
end
if string.len(text) > 0 then
delim = delim or ""
local pos = 1
-- if delim matches empty string then it would give an endless loop
if string.find("", delim, 1) and delim ~= "" then
error("delim matches empty string!")
end
local first, last
while 1 do
if delim ~= "" then
first, last = string.find(text, delim, pos)
else
first, last = string.find(text, "%s+", pos)
if first == 1 then
pos = last+1
first, last = string.find(text, "%s+", pos)
end
end
if first then -- found?
table.insert(list, string.sub(text, pos, first-1))
pos = last+1
else
table.insert(list, string.sub(text, pos))
break