Ruby equivalent to "exec > $logfile 2>&1" in sh script?

Discussion in 'Ruby' started by Phil Rhoades, Dec 1, 2006.

  1. Phil Rhoades

    Phil Rhoades Guest

    People,

    I quite frequently have something like:

    exec > $logfile 2>&1

    at the top of my shell scripts to output everything that follows (including
    errors) into a log file - is there some way of doing the equivalent in a Ruby
    script?

    Thanks,

    Phil.
    --
    Philip Rhoades

    Pricom Pty Limited (ACN 003 252 275 ABN 91 003 252 275)
    GPO Box 3411
    Sydney NSW 2001
    Australia
    Mobile: +61:(0)411-185-652
    Fax: +61:(0)2-8221-9599
    E-mail:
    Phil Rhoades, Dec 1, 2006
    #1
    1. Advertising

  2. Phil Rhoades

    Eric Hodel Guest

    On Nov 30, 2006, at 19:24 , El Gato wrote:
    > Phil Rhoades wrote:
    >> I quite frequently have something like:
    >>
    >> exec > $logfile 2>&1
    >>
    >> at the top of my shell scripts to output everything that follows
    >> (including errors) into a log file - is there some way of doing
    >> the equivalent in a Ruby script?

    >
    > This seems to work:
    >
    > outfile = File.open("output.txt", "w")
    > $stdout.reopen outfile
    > $stderr.reopen outfile
    >
    > puts "hello world!"
    > system("dir no_exist")


    Not really.

    $ ruby
    outfile = File.open("output.txt", "w")
    $stdout.reopen outfile
    $stderr.reopen outfile

    0.upto 10 do |v| (v%2==0 ? STDOUT : STDERR).puts v; end
    $ cat output.txt
    1
    3
    5
    7
    9
    0
    2
    4
    6
    8
    10


    --
    Eric Hodel - - http://blog.segment7.net

    I LIT YOUR GEM ON FIRE!
    Eric Hodel, Dec 1, 2006
    #2
    1. Advertising

  3. Phil Rhoades

    Rick Tessner Guest

    Hi all,

    Adding $stdout.sync = true; $stderr.sync = true should solve the problem
    of stdout and stderr being output of of sync.

    See in-line edit below.

    On Fri, 2006-01-12 at 16:10 +0900, Eric Hodel wrote:

    > $ ruby
    > outfile = File.open("output.txt", "w")
    > $stdout.reopen outfile
    > $stderr.reopen outfile


    $stdout.sync = true
    $stderr.sync = true

    > 0.upto 10 do |v| (v%2==0 ? STDOUT : STDERR).puts v; end



    --
    Rick
    Rick Tessner, Dec 1, 2006
    #3
    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. Rajat
    Replies:
    3
    Views:
    688
    Jorgen Grahn
    Jan 8, 2010
  2. Sy
    Replies:
    0
    Views:
    185
  3. Mario Gr
    Replies:
    3
    Views:
    298
    Mario Gr
    Jul 4, 2009
  4. Saravanan Sundaramoorthy

    how to test a ruby script from another ruby script?

    Saravanan Sundaramoorthy, Jul 17, 2010, in forum: Ruby
    Replies:
    1
    Views:
    143
    Roger Pack
    Jul 19, 2010
  5. 3lionz Wexler
    Replies:
    2
    Views:
    648
    Suraj Kurapati
    Aug 24, 2010
Loading...

Share This Page