2011-01-14 183 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