2011-01-14 180 views
0

这个问题在STDIN喂数据给多个外部命令是有点像我以前的(回答)问题:如何红宝石

How to run multiple external commands in the background in ruby

但是,在这种情况下,我正在寻找一种方式来养活Ruby字符串标准输入到外部进程,像这样(下面的代码是无效的,但说明我的目标):

#!/usr/bin/ruby 

str1 = 'In reality a relatively large string.....' 
str2 = 'Another large string' 
str3 = 'etc..' 

spawn 'some_command.sh', :stdin => str1 
spawn 'some_command.sh', :stdin => str2 
spawn 'some_command.sh', :stdin => str3 

Process.waitall 

回答

0

这似乎工作:

data = [str1, str2, str3] 

data.each do |input| 
    fork do 
    IO.popen(COMMAND, 'r+'){|n| n.print input} 
    end 
end 

Process.waitall 
+0

你不需要的。只是一个Thread.new应该工作。 – Luis 2011-01-14 11:21:30

0

我想输出从一个应用程序拆分到两个人,并不能得到tee工作。我使用了这个ruby脚本。

alpha = IO.popen("some shell command" , 'r+') 
bravo = IO.popen("other command" , 'r+') 

ARGF.each_line do |line| 
    alpha << line 
    bravo << line 
end