I have to use io.popen
in Lua to run an executable which takes a command line argument.
How to wait for a process to finish in the Lua so that expected output can be captured?
local command = "C:Program FilesXYZ.exe /all"
hOutput = io.popen(command)
print(string.format(""%s", hOutput))
Suppose the executable is XYZ.exe which needs to be called with command line argument /all
.
Once io.popen(command)
gets executed, the process will return some string which needs to be printed.
My code snippet:
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, '[
]+', ' ')
return s
end
local command = capture("C:Tester.exe /all")
Your help will be appreciated.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…