有一个os.spawn
函数在延期提案 http://lua-users.org/wiki/ExtensionProposal API.
您可以按如下方式使用它:
require"ex"
local proc, err = os.spawn{
command = e.."/bin/aprogr",
args = {
"arg1",
"arg2",
-- etc
},
env = {
A = 100, -- I assume it tostrings the value
B = "Hi",
C = "Test",
},
-- you can also specify stdin, stdout, and stderr
-- see the proposal page for more info
}
if not proc then
error("Failed to aprogrinate! "..tostring(err))
end
-- if you want to wait for the process to finish:
local exitcode = proc:wait()
lua 前派 http://code.google.com/p/lua-ex-api/提供 POSIX 和 Windows 的实现。
您可以找到与此实现捆绑在一起的预编译二进制文件Windows 下的 Lua http://code.google.com/p/luaforwindows/分配。
这是您的用例的更简洁版本:
require"ex"
local file = io.pipe()
local proc = assert(os.spawn(e.."/bin/aprogr", {
env={ A = 100, B = "Hi", C = "Test" },
stdout = file,
}))
-- write to file as you wish