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

后端 未结 5 2165
不知归路
不知归路 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:36

    There is a Linux-specific solution that does what I want:

    exec 9>&1
    case `readlink /dev/fd/9` in
        pipe:\[*\]) echo stdout is a pipe ;;
        *) echo stdout is not a pipe ;;
    esac
    

    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.

提交回复
热议问题