【发布时间】:2011-07-11 11:48:45
【问题描述】:
我必须在 Lua 中使用io.popen 来运行带有命令行参数的可执行文件。
如何在 Lua 中等待进程完成以便捕获预期的输出?
local command = "C:\Program Files\XYZ.exe /all"
hOutput = io.popen(command)
print(string.format(""%s", hOutput))
假设可执行文件是需要使用命令行参数/all 调用的XYZ.exe。
一旦io.popen(command)被执行,进程将返回一些需要打印的字符串。
我的代码sn-p:
function capture(cmd, raw)
local f = assert(io.popen(cmd, 'r'))
-- wait(10000);
local s = assert(f:read('*a'))
Print(string.format("String: %s",s ))
f:close()
if raw then return s end
s = string.gsub(s, '^%s+', '')
s = string.gsub(s, '%s+$', '')
s = string.gsub(s, '[\n\r]+', ' ')
return s
end
local command = capture("C:\Tester.exe /all")
您的帮助将不胜感激。
【问题讨论】:
-
我的代码不知何故无法正常工作
-
函数捕获(cmd, raw) local f = assert(io.popen(cmd, 'r')) -- wait(10000); local s = assert(f:read('*a')) Print(string.format("String: %s",s )) f:close() if raw then return s end s = string.gsub(s, '^%s+', '') s = string.gsub(s, '%s+$', '') s = string.gsub(s, '[\n\r]+', ' ') return s end本地命令 = capture("C:\Tester.exe /all")