lua-users home
lua-l archive

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


On 2/17/07, gary ng <garyng2000@yahoo.com> wrote:
Hi,

I am wondering if there is a simple way to do this,
that is popen("wc") then write things out to it and
read back the result.

Not with popen.  However, with the "ex" API, you can implement this
easily enough:

require "ex"

function popen2(...)
 local in_rd, in_wr = io.pipe()
 local out_rd, out_wr = io.pipe()
 local proc, err = os.spawn{stdin = in_rd, stdout = out_wr, ...}
 in_rd:close(); out_wr:close()
 if not proc then
   in_wr:close(); out_rd:close()
   return proc, err
 end
 return proc, out_rd, in_wr
end

local p, i, o = assert(popen2("wc", "-w"))
o:write("Hello world"); o:close()
print(i:read"*l"); i:close()
p:wait()

See http://lua-users.org/wiki/ExtensionProposal

    -Mark