lua-users home
lua-l archive

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

Manfred Lotz wrote:

> Tried it out. At least on my platform (FreeBSD 6.0) ErrStr
> isn't used in case an error happens. The error message
> just goes to stderr

ErrStr will be set (and Hnd will be nil) only if the pipe
itself is not successfully opened (e.g.,  if you pass a
non-string argument to io.popen, or if it's not supported on
your platform).  If the command you're executing (or the
shell, if it can't find the command) writes to stderr, that
will come out on the Lua program's stderr.  To avoid this,
redirect stderr to stdout:

  local Hnd, ErrStr = io.popen("somecommand 2>&1")

If the command reads from stdin, it will read from the Lua
program's stdin.  If you want to write to the pipe instead
of (but not in addition to) reading it, give io.popen a
second argument of "w":

  local Hnd, ErrStr = io.popen("somecommand 2>&1", "w")

In this case, if the command writes to stdout, it will come
out on the Lua program's stdout (unless redirected).

See src/lib/liolib.c (in the Lua distribution) and
man 3 popen for more details.