开发者

How to feed data over STDIN to multiple external commands in ruby

开发者 https://www.devze.com 2023-02-04 09:09 出处:网络
This question is a bit lik开发者_StackOverflow社区e my previous (answered) question: How to run multiple external commands in the background in ruby

This question is a bit lik开发者_StackOverflow社区e my previous (answered) question:

How to run multiple external commands in the background in ruby

But, in this case I am looking for a way to feed ruby strings over STDIN to external processes, something like this (the code below is not valid but illustrates my goal):

#!/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


This seems to work:

data = [str1, str2, str3]

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

Process.waitall


I wanted to split the output from one app to two others and couldn't get tee to work. I resorted to this ruby script.

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

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

精彩评论

暂无评论...
验证码 换一张
取 消