情景:
我必须从我的Ruby脚本调用外部程序,并且该程序向stdout和stderr发送了许多有用的(但含糊不清的)信息。
当程序运行时,我想解析它发送给stdout和stderr的行:
我尝试了所有常用的技巧(系统,exec,popen,popen3,反引号等等),但我只能在执行程序后检索stdout / stderr ,而不是 执行期间。
有什么想法吗?
哦,我在Windows上: - (
答案 0 :(得分:15)
实际上,它比我想象的要简单,这似乎完美无缺:
STDOUT.sync = true # That's all it takes...
IO.popen(command+" 2>&1") do |pipe| # Redirection is performed using operators
pipe.sync = true
while str = pipe.gets
puts "-> "+str # This is synchronous!
end
end
......是的,它适用于Windows!