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
:)