您当前的位置:首页 > 计算机 > 系统应用 > Linux

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

时间:12-14来源:作者:点击数:

使用 -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

方便获取更多学习、工作、生活信息请关注本站微信公众号城东书院 微信服务号城东书院 微信订阅号
推荐内容
相关内容
栏目更新
栏目热门
本栏推荐