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

后端 未结 5 2185
不知归路
不知归路 2021-02-14 21:07

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

    In Python:

    import sys
    print sys.stdout.isatty()
    

    Examples:

    $ ./a
    True
    $ ./a | less
    False
    

提交回复
热议问题