redirect stdout for Kernel.system()?

Discussion in 'Ruby' started by Neil Spring, Dec 16, 2003.

  1. Neil Spring

    Neil Spring Guest

    I'd like to be able to say:

    $stdout=File.open("foo.out", "w")
    Kernel.system("/bin/echo", "foo")

    intead of:

    Kernel.system("/bin/echo foo > foo.out")

    which requires two processes; forking a "/bin/sh" for
    something as simple as redirection is sad.

    In ruby1.6.8, this works. In ruby1.8.1 and current cvs, the
    behavior is to redirect puts output to the file (yay!), but
    the stdout of the child process is not affected (boo.).

    I think it is valuable to have the output of system() go to
    the currently defined $stdout. From before I found ruby,
    perl will allow 'open(STDOUT, ">ick.out"); system("/bin/echo
    perl is icky")'.

    Is this a bug, intended behavior, or known ambiguity?
    Is there an alternative method I haven't thought of?

    (test process output follows, maybe it's just me.)

    thanks,
    -neil


    (in the output below, the duplicated messages are probably
    due to buffering during the fork)

    rf:/tmp> cat ~/foo.rb
    #!/usr/bin/ruby

    $stdout=File.open("foo.out", "w")
    puts "fooble"
    Kernel.system("echo", "foo")

    Thread.new {
    $stdout=File.open("bar.out", "w")
    puts "barble"
    Kernel.system("echo", "bar")
    }.join

    Process.fork {
    $stdout=File.open("baz.out", "w")
    puts "bazzle"
    Kernel.system("echo", "baz")
    }
    Process.wait

    rf:/tmp> rm *.out

    rf:/tmp> ruby1.8 ~/foo.rb
    foo
    bar
    baz

    rf:/tmp> cat baz.out
    bazzle

    rf:/tmp> cat bar.out
    barble
    barble

    rf:/tmp> cat foo.out
    fooble
    fooble

    rf:/tmp> ruby1.8 --version
    ruby 1.8.1 (2003-11-11) [i386-linux]

    rf:/tmp> ruby --version
    ruby 1.6.8 (2003-07-09) [i386-linux]

    rf:/tmp> rm *.out

    rf:/tmp> ruby ~/foo.rb

    rf:/tmp> cat *.out
    barble
    bar
    bazzle
    baz
    fooble
    foo
    Neil Spring, Dec 16, 2003
    #1
    1. Advertising

  2. You can use IO.popen for that:
    http://whytheluckystiff.net/ruby/pickaxe/html/ref_c_io.html#IO.popen

    robert

    "Neil Spring" <> schrieb im Newsbeitrag
    news:...
    > I'd like to be able to say:
    >
    > $stdout=File.open("foo.out", "w")
    > Kernel.system("/bin/echo", "foo")
    >
    > intead of:
    >
    > Kernel.system("/bin/echo foo > foo.out")
    >
    > which requires two processes; forking a "/bin/sh" for
    > something as simple as redirection is sad.
    >
    > In ruby1.6.8, this works. In ruby1.8.1 and current cvs, the
    > behavior is to redirect puts output to the file (yay!), but
    > the stdout of the child process is not affected (boo.).
    >
    > I think it is valuable to have the output of system() go to
    > the currently defined $stdout. From before I found ruby,
    > perl will allow 'open(STDOUT, ">ick.out"); system("/bin/echo
    > perl is icky")'.
    >
    > Is this a bug, intended behavior, or known ambiguity?
    > Is there an alternative method I haven't thought of?
    >
    > (test process output follows, maybe it's just me.)
    >
    > thanks,
    > -neil
    >
    >
    > (in the output below, the duplicated messages are probably
    > due to buffering during the fork)
    >
    > rf:/tmp> cat ~/foo.rb
    > #!/usr/bin/ruby
    >
    > $stdout=File.open("foo.out", "w")
    > puts "fooble"
    > Kernel.system("echo", "foo")
    >
    > Thread.new {
    > $stdout=File.open("bar.out", "w")
    > puts "barble"
    > Kernel.system("echo", "bar")
    > }.join
    >
    > Process.fork {
    > $stdout=File.open("baz.out", "w")
    > puts "bazzle"
    > Kernel.system("echo", "baz")
    > }
    > Process.wait
    >
    > rf:/tmp> rm *.out
    >
    > rf:/tmp> ruby1.8 ~/foo.rb
    > foo
    > bar
    > baz
    >
    > rf:/tmp> cat baz.out
    > bazzle
    >
    > rf:/tmp> cat bar.out
    > barble
    > barble
    >
    > rf:/tmp> cat foo.out
    > fooble
    > fooble
    >
    > rf:/tmp> ruby1.8 --version
    > ruby 1.8.1 (2003-11-11) [i386-linux]
    >
    > rf:/tmp> ruby --version
    > ruby 1.6.8 (2003-07-09) [i386-linux]
    >
    > rf:/tmp> rm *.out
    >
    > rf:/tmp> ruby ~/foo.rb
    >
    > rf:/tmp> cat *.out
    > barble
    > bar
    > bazzle
    > baz
    > fooble
    > foo
    >
    >
    >
    Robert Klemme, Dec 16, 2003
    #2
    1. Advertising

  3. Neil Spring

    Guest

    Hi,

    At Tue, 16 Dec 2003 15:53:43 +0900,
    Neil Spring wrote:
    > I'd like to be able to say:
    >
    > $stdout=File.open("foo.out", "w")
    > Kernel.system("/bin/echo", "foo")
    >
    > intead of:
    >
    > Kernel.system("/bin/echo foo > foo.out")
    >
    > which requires two processes; forking a "/bin/sh" for
    > something as simple as redirection is sad.


    $stdout.reopen("foo.out")

    --
    Nobu Nakada
    , Dec 16, 2003
    #3
  4. Neil Spring

    Neil Spring Guest

    On Wed, Dec 17, 2003 at 03:47:52AM +0900, wrote:
    > Hi,


    >> I'd like to be able to say:
    >>
    >> $stdout=File.open("foo.out", "w")
    >> Kernel.system("/bin/echo", "foo")
    >>


    > $stdout.reopen("foo.out")


    thanks! that was exactly what I needed.

    -neil
    Neil Spring, Dec 19, 2003
    #4
  5. On Tue, 16 Dec 2003 15:53:43 +0900, Neil Spring <>
    wrote:
    > I'd like to be able to say:
    >
    > $stdout=File.open("foo.out", "w")
    > Kernel.system("/bin/echo", "foo")


    This gives the desired behavior with 1.8.1p3 and Linux:

    oldout = $stdout.dup

    $stdout.reopen("foo.out", "w")
    puts "fooble"
    Kernel.system("echo", "foo")

    $stdout.reopen(oldout)
    puts 'back to normal'
    Kernel.system("echo", "done")

    regards,
    andrew
    Andrew Johnson, Dec 24, 2003
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. yogesh
    Replies:
    3
    Views:
    585
    Kenny McCormack
    Feb 12, 2006
  2. Replies:
    3
    Views:
    166
  3. Andreas S
    Replies:
    3
    Views:
    261
    Eric Hodel
    Dec 9, 2006
  4. Gordon Thiesfeld

    Kernel#putc vs. $stdout#putc

    Gordon Thiesfeld, Feb 20, 2008, in forum: Ruby
    Replies:
    3
    Views:
    104
    Arlen Cuss
    Feb 21, 2008
  5. Lars Gierth
    Replies:
    6
    Views:
    217
    David Masover
    Mar 20, 2010
Loading...

Share This Page