我的 Ruby 脚本正在运行一个 shell 命令并解析它的输出。但是,似乎该命令首先执行并将输出保存在一个数组中。我希望能够在打印时实时访问输出行。我玩过线程,但没有得到它的工作。有什么建议么?
问问题
3908 次
2 回答
21
您正在寻找管道。这是一个例子:
# This example runs the netstat command via a pipe
# and processes the data in Ruby as it come back
pipe = IO.popen("netstat 3")
while (line = pipe.gets)
print line
print "and"
end
于 2010-04-29T22:50:57.077 回答
0
当调用方法/函数来运行系统/shell命令时,你的解释器会产生另一个进程来运行它并等待它完成,然后给你输出。
即使您使用线程,您唯一要做的就是在命令运行时不要让程序挂起,但在完成之前您仍然不会得到输出。
我认为您可以使用管道来实现这一点,但我不确定如何。
@Marcel 明白了。
于 2010-04-29T22:52:19.777 回答