Detection of redirection, possible?

Discussion in 'Ruby' started by Shannon Fang, Oct 27, 2005.

  1. Shannon Fang

    Shannon Fang Guest

    Hi All,

    How can I detect a ruby program's stdio has been redirected? I would like to
    do this:

    puts "show message to user"
    $stdin.gets unless $stdin.redirected?

    This way, if the program runs normally, it show message and wait for user
    interaction, if it is redirected to a file, then just puts to that file
    without wating for user intervention.

    Thanks!
    Shannon
     
    Shannon Fang, Oct 27, 2005
    #1
    1. Advertising

  2. Shannon Fang

    Eric Hodel Guest

    On Oct 26, 2005, at 11:44 PM, Shannon Fang wrote:

    > Hi All,
    >
    > How can I detect a ruby program's stdio has been redirected? I
    > would like to do this:
    >
    > puts "show message to user"
    > $stdin.gets unless $stdin.redirected?
    >
    > This way, if the program runs normally, it show message and wait
    > for user interaction, if it is redirected to a file, then just puts
    > to that file without wating for user intervention.


    I think you want IO#tty?:

    $ ruby -e 'p $stdin.tty?'
    true
    $ ruby -e 'p $stdin.tty?' < /dev/zero
    false

    --
    Eric Hodel - - http://segment7.net
    FEC2 57F1 D465 EB15 5D6E 7C11 332A 551C 796C 9F04
     
    Eric Hodel, Oct 27, 2005
    #2
    1. Advertising

  3. Hi,

    At Thu, 27 Oct 2005 15:44:54 +0900,
    Shannon Fang wrote in [ruby-talk:162881]:
    > puts "show message to user"

    $stdin.gets if $stdin.tty?

    --
    Nobu Nakada
     
    nobuyoshi nakada, Oct 27, 2005
    #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. Carlos
    Replies:
    4
    Views:
    21,906
    amimpat
    Aug 11, 2009
  2. jaydev

    browser detection and redirection

    jaydev, Oct 23, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    442
    Mark Rae
    Oct 23, 2006
  3. jaydev
    Replies:
    41
    Views:
    1,492
    Chaddy2222
    Oct 27, 2006
  4. Replies:
    3
    Views:
    241
    Carl Banks
    Jan 14, 2007
  5. jaydev

    browser detection and redirection

    jaydev, Oct 23, 2006, in forum: ASP .Net Web Services
    Replies:
    0
    Views:
    121
    jaydev
    Oct 23, 2006
Loading...

Share This Page