From: Jason Roelofs Date: 2007-08-03T01:46:43+09:00 Subject: Re: Piping two shell commands together ------=_Part_22888_19369220.1186073208348 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline `#{cmd1.join(' ')} | #{cmd2.join(' ')}` Those are backticks. Let the system handle piping with the '|' character. Jason On 8/2/07, Hans Fugal wrote: > > I want to pipe two system commands together from within ruby. cmd1 and > cmd2 are arrays, e.g. > > cmd1 = ['oggdec','-o','-',oldpath] > cmd2 = ['lame','-',newpath] > > I think I can do the following, > > IO.popen('-','r') do |p1| > exec cmd1 unless p1 > IO.popen('-','w') do |p2| > exec cmd2 unless p2 > p2.write(p1.read) > end > end > > but I don't like the line "p2.write(p1.read)", because I think it will > read into memory from p1 before turning around and writing it to p2. > This may work for the above example, but it's not very pipelike and I > worry that read might return early (I'm not sure what the semantics of > that are in Ruby, but in C I'd have to account for that). > > Plus I don't like the popen '-'/exec combination. I'd love a more > elgant, efficient, and/or safer idiom if anyone can think of one. > > ------=_Part_22888_19369220.1186073208348--