Detect if stdout is redirected to a pipe (not to a file, character device, terminal, or socket)?

后端 未结 5 873
粉色の甜心
粉色の甜心 2021-02-14 20:50

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

5条回答
  •  攒了一身酷
    2021-02-14 21:31

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

提交回复
热议问题