场景:
我必须从我的Ruby脚本中调用一个外部程序,并且这个程序向stdout和stderr发送了很多有用的(但是神秘的)信息。
程序运行时 ,我想分析它发送到stdout和stderr的行,并且:
我尝试了所有常用的技巧(系统,exec,popen,popen3,反引号等等),但是我只能在程序执行后取回stdout / stderr,而不是执行过程中。
有任何想法吗?
哦,我在Windows上:-(
其实,这比我想象的更简单,这似乎是完美的:
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!