本文介绍了如何使用lua构造读写管道?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想做的等同于:
foo=$(echo "$foo"|someprogram)
在lua内-即,我有一个包含一堆文本的变量,我想通过一个过滤器运行它(发生在python中).
within lua -- ie, I've got a variable containing a bunch of text, and I'd like to run it through a filter (implemented in python as it happens).
有任何提示吗?
已添加:非常想在不使用临时文件的情况下完成此操作
Added: would really like to do this without using a temporary file
推荐答案
哈哈,可能是一个更好的解决方案:
Aha, a possibly better solution:
require('posix')
require('os')
require('io')
function splat_popen(data,cmd)
rd,wr = posix.pipe()
io.flush()
child = posix.fork()
if child == 0 then
rd:close()
wr:write(data)
io.flush()
os.exit(1)
end
wr:close()
rd2,wr2 = posix.pipe()
io.flush()
child2 = posix.fork()
if child2 == 0 then
rd2:close()
posix.dup(rd,io.stdin)
posix.dup(wr2,io.stdout)
posix.exec(cmd)
os.exit(2)
end
wr2:close()
rd:close()
y = rd2:read("*a")
rd2:close()
posix.wait(child2)
posix.wait(child)
return y
end
munged=splat_popen("hello, world","/usr/games/rot13")
print("munged: "..munged.." !")
这篇关于如何使用lua构造读写管道?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!