How can I know if STDOUT has been redirected?

Discussion in 'Perl Misc' started by Michele Dondi, Oct 20, 2004.

  1. As of the subject, I'd like to know if there's a way for a program to
    understand wether STDOUT is connected to a tty or has been redirected,
    like e.g. ls with --color=auto does. (Of course this is OS specific;
    I'm specifically interested in the Linux case.)


    Michele
    --
    {$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
    (($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
    ..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
    256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
    Michele Dondi, Oct 20, 2004
    #1
    1. Advertising

  2. Michele Dondi

    Anno Siegel Guest

    Michele Dondi <> wrote in comp.lang.perl.misc:
    > As of the subject, I'd like to know if there's a way for a program to
    > understand wether STDOUT is connected to a tty or has been redirected,
    > like e.g. ls with --color=auto does. (Of course this is OS specific;
    > I'm specifically interested in the Linux case.)


    The file test -t checks if a filehandle is open to a tty. It does
    not check whether the handle has been redirected -- redirection is
    a shell action that simply offers the other program the named file
    as stdin (or stdout, or stderr). There is no way you can look at
    a filehandle and decide whether it was opened due to a redirect
    action.

    Anno
    Anno Siegel, Oct 20, 2004
    #2
    1. Advertising

  3. Michele Dondi

    Big and Blue Guest

    Anno Siegel wrote:
    >
    > The file test -t checks if a filehandle is open to a tty. It does
    > not check whether the handle has been redirected -- redirection is
    > a shell action that simply offers the other program the named file
    > as stdin (or stdout, or stderr).


    Well, if it has been redirected it will no longer be attached to a tty
    (unless you redirected it to /dev/tyy, or redirected stderr to stdout)! (I
    was recently looking into the possibility of running a sub-process with its
    stdin/stdout attached to the parent tty, while the sub-process had its own
    tty, but couldn't see anyway to achieve it, since the only way to open a
    tty was to close all references to the current one first - but this is
    nothing to do with Perl).

    But the original question wasactually about it being a tty anyway...


    --
    -*- Just because I've written it here doesn't -*-
    -*- mean that you should, or I do, believe it. -*-
    Big and Blue, Oct 21, 2004
    #3
  4. On 20 Oct 2004 13:20:15 GMT, -berlin.de (Anno
    Siegel) wrote:

    >The file test -t checks if a filehandle is open to a tty. It does
    >not check whether the handle has been redirected -- redirection is
    >a shell action that simply offers the other program the named file
    >as stdin (or stdout, or stderr). There is no way you can look at
    >a filehandle and decide whether it was opened due to a redirect
    >action.


    TY, I suspected that the answer would have been somewhere in the docs.
    Only buried a little deeper...


    Michele
    --
    {$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
    (($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
    ..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
    256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
    Michele Dondi, Oct 21, 2004
    #4
    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. Bruno Thomas
    Replies:
    2
    Views:
    1,291
    Bruno Thomas
    Sep 13, 2005
  2. Michele Simionato
    Replies:
    11
    Views:
    436
    vasudevram
    Dec 23, 2006
  3. Bruno Desthuilliers
    Replies:
    12
    Views:
    455
    Raymond Hettinger
    Sep 24, 2007
  4. Replies:
    2
    Views:
    102
  5. Replies:
    2
    Views:
    314
    A. Sinan Unur
    Dec 7, 2005
Loading...

Share This Page