lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Mike Vidal wrote:

> How do I capture the output of a command line program with
> os.execute ?  Is the only way to redirect the commands
> output to a file then read that?

Basically, yes.

If the proper switch was flipped at compilation (look for
"POPEN" in the config file), and if it's supported by your
platform, Lua 5.0.2 has the undocumented function io.popen,
which returns a read-only file handle from which you can
read the output of a command:

local Hnd, ErrStr = io.popen("ls -la")
if Hnd then
  for Line in Hnd:lines() do
    print(Line)
  end -- for Line
  Hnd:close()
else
  print(ErrStr)
end -- if

-- 
Aaron