18

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 Files\XYZ.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, '[\n\r]+', ' ') 
  return s 
end 
local command = capture("C:\Tester.exe /all")

Your help will be appreciated.

greatwolf
  • 20,287
  • 13
  • 71
  • 105
Chet
  • 193
  • 1
  • 1
  • 5
  • I am having a code somehow it is not working properly – Chet Mar 08 '11 at 23:19
  • 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") – Chet Mar 08 '11 at 23:20

2 Answers2

25

If you are using standard Lua your code looks a bit odd. I am not completely sure about io.popen semantics regarding timeouts or platform dependencies, but the following works at least on my machine.

local file = assert(io.popen('/bin/ls -la', 'r'))
local output = file:read('*all')
file:close()
print(output) -- > Prints the output of the command.
rubo77
  • 19,527
  • 31
  • 134
  • 226
ponzao
  • 20,684
  • 3
  • 41
  • 58
  • 1
    can you explain, why you need the `assert` around? – rubo77 Feb 22 '19 at 11:30
  • @rubo77, I haven't used Lua in a while, but based on a quick test I don't think the `assert` is at all useful in this case as `io.popen` seems to return an object even when there is an error. – ponzao Feb 23 '19 at 19:51
3

I ended up with this for capturing relatively big output:

io.stdout:setvbuf 'no' 
local file = assert(io.popen('/bin/ls -la', 'r'))
file:flush()  -- > important to prevent receiving partial output
local output = file:read('*all')
file:close()
coderofsalvation
  • 1,764
  • 16
  • 13