8

我有一个 bash 脚本,它提示用户输入“读取”。如果 stdout 或 stderr 被传送到终端以外的其他东西,我想禁止这一步。那可能吗?

4

1 回答 1

16

您可以使用命令 test -t <filedescriptor no.> 检查文件描述符是否为 tty(附加到终端)。如果是,您可以提示用户。如果不是,则输出可能在某处通过管道传输或重定向。

if test -t 1  ; then
  echo stdout is a tty
fi
于 2009-07-29T22:02:15.237 回答