lua 获取shell执行结果 进行字符串分割
程序员文章站
2024-02-12 13:32:16
...
function popen_command_info(command)
local command_info
local command_info_fd = io.popen(command, "r")
if command_info_fd then
while true do
command_info = command_info_fd:read("*l")
if command_info then break end
if not command_info then
command_info = ""
break
end
end
command_info_fd:close()
end^M
return command_info
end
function Split(szFullString, szSeparator)
local nFindStartIndex = 1
local nSplitIndex = 1
local nSplitArray = {}
while true do
local nFindLastIndex = string.find(szFullString, szSeparator, nFindStartIndex)
if not nFindLastIndex then
nSplitArray[nSplitIndex] = string.sub(szFullString, nFindStartIndex, string.len(szFullString))
break
end
nSplitArray[nSplitIndex] = string.sub(szFullString, nFindStartIndex, nFindLastIndex - 1)
nFindStartIndex = nFindLastIndex + string.len(szSeparator)
nSplitIndex = nSplitIndex + 1
end
return nSplitArray
end
local cmdres=popen_command_info(cmd)
local list = Split("abc,123,345", ",")