4

如何在bash脚本中判断输入输出是否被重定向到文件

 2 years ago
source link: https://www.lujun9972.win/blog/2018/04/26/%E5%A6%82%E4%BD%95%E5%9C%A8bash%E8%84%9A%E6%9C%AC%E4%B8%AD%E5%88%A4%E6%96%AD%E8%BE%93%E5%85%A5%E8%BE%93%E5%87%BA%E6%98%AF%E5%90%A6%E8%A2%AB%E9%87%8D%E5%AE%9A%E5%90%91%E5%88%B0%E6%96%87%E4%BB%B6/index.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

如何在bash脚本中判断输入输出是否被重定向到文件

使用 -t FD 能判断文件描述符FD是否与终端相连,因此

if [[ -t 0 ]];then
    echo "stdin is a terminal"
else
    echo "stdin is not a terminal"
fi

if [[ -t 1 ]];then
    echo "stdout is a terminal"
else
    echo "stdout is not a terminal"
fi

if [[ -t 2 ]];then
    echo "stderr is a terminal"
else
    echo "stderr is not a terminal"
fi

stdin is not a terminal stdout is not a terminal stderr is not a terminal


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK