On Thu, 12 May 2005, Lawrence Oluyede wrote:

> What I'm trying to do is to pass some data
> to a child process that execute an external process
> and get back data from this external process.
>
> ---
> PATH = "/usr/bin/highlight"
> ARGS = "-f -l -t 8 -S %s"
> LANG = "rb"
>
> rd, wr = IO.pipe
>
> if fork
>  # parent
>  rd.close
>  $stdout.reopen(wr)
>  wr.close
>
>  fd = File.open("/usr/lib/ruby/1.8/thread.rb")
>  str = fd.read
>  $stdout.write str
> else
>  # child
>  wr.close
>  $stdin.reopen(rd)
>  rd.close
>
>  params = ARGS % LANG
>  cmd = "%s %s" % [PATH, params]
>  exec cmd
> end

   harp:~ > cat a.rb
   IO::popen('-') do |pipe|
     if pipe
       stdout = pipe.read
       puts "parent got <#{ stdout }> from child"
     else
       exec 'echo', '-n', '42'
     end
   end

   harp:~ > ruby a.rb
   parent got <42> from child


you can also

   STDERR.reopen '/dev/null'

in the child the shutup stderr - otherwise look at the code for open3 (it's
short) to see how to get a handle on both stdout and stderr of the child.

hth.

-a
-- 
===============================================================================
| email :: ara [dot] t [dot] howard [at] noaa [dot] gov
| phone :: 303.497.6469
| renunciation is not getting rid of the things of this world, but accepting
| that they pass away. --aitken roshi
===============================================================================