【发布时间】:2022-01-20 07:30:53
【问题描述】:
我希望我的代码中有一个部分仅在终端打印输出时执行,而不是通过管道传输或重定向到文本文件或其他程序中。
我试过这个:
#!/bin/bash
if [[ $(tty "-s") ]]; then
printf "You are using a terminal.\n"
else
printf "You are not using a terminal.\n"
fi
tty -s && printf "Guten Tag.\n"
./tty.sh 命令后的输出:
You are not using a terminal.
Guten Tag.
在 `./tty.sh > test.txt` 之后的 test.txt 中输出:
``` 你没有使用终端。 古腾标签。 ```
【问题讨论】:
-
这里的基本语法部分是 stackoverflow.com/questions/36371221/… 的副本 - 您正在测试
tty -s是否输出非空字符串,它根本不会输出。