[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: popen to a "pipe" utility ?
- From: "Mark Edgar" <medgar123@...>
- Date: Sat, 17 Feb 2007 00:51:09 -0700
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