title: io.popen - how to wait for process to finish in Lua?
body: <p>I have to use <code>io.popen</code> 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?</p>
  
  <pre><code>  local command = "C:\Program Files\XYZ.exe /all"
  
    hOutput = io.popen(command)
    print(string.format(""%s", hOutput))
  </code></pre>
  
  <p>Suppose the executable is XYZ.exe which needs to be called with command line argument <code>/all</code>.  </p>
  
  <p>Once <code>io.popen(command)</code> gets executed, the process will return some string which needs to be printed.</p>
  
  <p>My code snippet:</p>
  
  <pre><code>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")
  </code></pre>
  
  <p>Your help will be appreciated.</p>