Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.ruby > #4540

Re: Shell pipeline in Ruby?

From Brian Candler <b.candler@pobox.com>
Newsgroups comp.lang.ruby
Subject Re: Shell pipeline in Ruby?
Date 2011-05-14 10:14 -0500
Organization Service de news de lacave.net
Message-ID <c3237639632bb3753c86f07b7056a5fd@ruby-forum.com> (permalink)
References <BANLkTik15O2NLu4grCfhbtF3Ws6N_c4Qtg@mail.gmail.com>

Show all headers | View raw


Here is a ruby 1.9.2 version with close_on_exec=. I leave it as an 
exercise to refactor this to an arbitrary pipeline of N commands - it 
would return one input pipe, one output pipe, N pids and N error pipes.

Regards,

Brian.


#Thread.abort_on_exception = true  # for debugging

ruby_to_a_rd, ruby_to_a_wr = IO.pipe
ruby_to_a_wr.close_on_exec = true # no children should have this

a_to_b_rd, a_to_b_wr = IO.pipe

a_err_rd, a_err_wr = IO.pipe
a_err_rd.close_on_exec = true     # no children should have this

pid1 = fork do
  a_to_b_rd.close
  STDIN.reopen(ruby_to_a_rd)
  STDOUT.reopen(a_to_b_wr)
  STDERR.reopen(a_err_wr)
  exec("cat; echo done cat 1>&2")
  STDERR.puts "Whoops! #{$!}"
end
# we don't want these fds, nor any of the further children
ruby_to_a_rd.close
a_to_b_wr.close
a_err_wr.close

b_to_c_rd, b_to_c_wr = IO.pipe
b_err_rd, b_err_wr = IO.pipe
b_err_rd.close_on_exec = true  # no children should have this

pid2 = fork do
  b_to_c_rd.close
  STDIN.reopen(a_to_b_rd)
  STDOUT.reopen(b_to_c_wr)
  STDERR.reopen(b_err_wr)
  exec("tr 'a-z' 'A-Z'; echo done tr 1>&2")
  STDERR.puts "Whoops! #{$!}"
end
# we don't want these fds, nor any of the further children
a_to_b_rd.close
b_to_c_wr.close
b_err_wr.close

c_to_ruby_rd, c_to_ruby_wr = IO.pipe
c_err_rd, c_err_wr = IO.pipe
c_err_rd.close_on_exec = true  # no children should have this
pid3 = fork do
  c_to_ruby_rd.close
  STDIN.reopen(b_to_c_rd)
  STDOUT.reopen(c_to_ruby_wr)
  STDERR.reopen(c_err_wr)
  exec("sed 's/O/0/g'; echo done sed 1>&2")
  STDERR.puts "Whoops! #{$!}"
end
# we don't want these fds, nor any of the further children
b_to_c_rd.close
c_to_ruby_wr.close
c_err_wr.close

Thread.new do
  ruby_to_a_wr.puts "Here is some data"
  ruby_to_a_wr.puts "And some more"
  ruby_to_a_wr.close
end

Thread.new do
  while line = a_err_rd.gets
    puts "A err: #{line}"
  end
  a_err_rd.close
end

Thread.new do
  while line = b_err_rd.gets
    puts "B err: #{line}"
  end
  b_err_rd.close
end

Thread.new do
  while line = c_err_rd.gets
    puts "C err: #{line}"
  end
  c_err_rd.close
end

while line = c_to_ruby_rd.gets
  puts line
end
c_to_ruby_rd.close

-- 
Posted via http://www.ruby-forum.com/.

Back to comp.lang.ruby | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Shell pipeline in Ruby? Michal Suchanek <hramrach@centrum.cz> - 2011-05-12 11:05 -0500
  Re: Shell pipeline in Ruby? Jos Backus <jos@catnook.com> - 2011-05-12 12:22 -0500
    Re: Shell pipeline in Ruby? Michal Suchanek <hramrach@centrum.cz> - 2011-05-12 14:41 -0500
      Re: Shell pipeline in Ruby? Stu <stu@rubyprogrammer.net> - 2011-05-12 14:54 -0500
        Re: Shell pipeline in Ruby? Michal Suchanek <hramrach@centrum.cz> - 2011-05-12 15:15 -0500
  Re: Shell pipeline in Ruby? 7stud -- <bbxx789_05ss@yahoo.com> - 2011-05-12 13:06 -0500
  Re: Shell pipeline in Ruby? Roger Pack <rogerpack2005@gmail.com> - 2011-05-12 22:13 -0500
    Re: Shell pipeline in Ruby? Michal Suchanek <hramrach@centrum.cz> - 2011-05-13 03:33 -0500
      Re: Shell pipeline in Ruby? Roger Pack <rogerpack2005@gmail.com> - 2011-05-13 08:44 -0500
    Re: Shell pipeline in Ruby? Roger Pack <rogerpack2005@gmail.com> - 2011-05-13 08:48 -0500
      Re: Shell pipeline in Ruby? Michal Suchanek <hramrach@centrum.cz> - 2011-05-13 13:14 -0500
  Re: Shell pipeline in Ruby? Robert Klemme <shortcutter@googlemail.com> - 2011-05-13 03:44 -0500
    Re: Shell pipeline in Ruby? Michal Suchanek <hramrach@centrum.cz> - 2011-05-13 08:27 -0500
      Re: Shell pipeline in Ruby? Robert Klemme <shortcutter@googlemail.com> - 2011-05-13 09:23 -0500
        Re: Shell pipeline in Ruby? Michal Suchanek <hramrach@centrum.cz> - 2011-05-13 12:44 -0500
  Re: Shell pipeline in Ruby? Michal Suchanek <hramrach@centrum.cz> - 2011-05-13 10:32 -0500
    Re: Shell pipeline in Ruby? Brian Candler <b.candler@pobox.com> - 2011-05-14 10:05 -0500
      Re: Shell pipeline in Ruby? Michal Suchanek <hramrach@centrum.cz> - 2011-05-16 06:05 -0500
  Re: Shell pipeline in Ruby? Brian Candler <b.candler@pobox.com> - 2011-05-14 10:14 -0500
  Re: Shell pipeline in Ruby? Michal Suchanek <hramrach@centrum.cz> - 2011-05-16 10:29 -0500

csiph-web