Ideally, this would be scriptable in shell, but Perl or Python would be fine. C code could be helpful, but probably fails cost/benefit.
I recognize that redirection to a FIFO (named pipe) may be indistinguishable from a real pipe, and that is enough of an edge case that I don't really care.
Strict POSIX solutions are best, UNIX/Linux variant-independent are next best, but at least something that works on Darwin (MacOS X) is what I need right now.
Before you write your answer - I already know about test -t
- that will tell me whether stdout is a terminal (in which case it is definitely not a pipe - but it will not tell me whether stdout has been redirected to a file, non-terminal character device, or UNIX-domain socket rather than a pipe.
Intended use case: I have a command that should be run within backquotes in the shell,
so that it can output commands that set environment variables.
I would like the command to abort with an error if stdout is not redirected to a pipe,
as in that case it definitely wasn't invoked by eval `mycommand`;
.
If there's some special environment variable that a shell will set when running a command within backquotes that would be helpful, but as it is likely to be specific to bash or zsh or something, pipe-detection is more important.
(my comment turned to answer per Alex Dupuy's suggestion) Since you said you can use perl, I guess you can use its -p file test operator (perldoc.perl.org/functions/-X.html); something like
perl -e 'exit(-p STDOUT ? 0 : 1);'
will tell you if stdout if a pipe or a fifo (without distinguishing between them).Fwiw, calling that from the shell is exactly the reason why I used
perl -e
:)The
stat
command works on MacOS X and (with some finessing of variant implementations) other Linux/Unix variants as well, so it is a solution for me:You could do an
fstat
to the file descriptor and check returning structure, for example,st_mode
= 0x1000 (S_IFIFO) indicates a Named Pipe.Example with Python:
Output on windows:
There is a Linux-specific solution that does what I want:
but that isn't the answer I am looking for as I want this to run on xBSD as well.
I've made this answer community wiki, so feel free to improve it, but if you have a non-Linux solution (or a Linux-specific solution using another technique), please create a new answer.
In Python:
Examples: