How can I know if STDOUT has been redirected?

M

Michele Dondi

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
 
A

Anno Siegel

Michele Dondi said:
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
 
B

Big and Blue

Anno said:
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...
 
M

Michele Dondi

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top