在bash脚本中测试stdout和stderr重定向 [英] Test stdout and stderr redirection in bash script

查看:102
本文介绍了在bash脚本中测试stdout和stderr重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的bash脚本中测试定向stdout和stderr的位置,或更准确地说,是否已重定向它们.

I would like to test in my bash script where stdout and stderr are directed, or more precisely, if they have been redirected.

你有个主意吗?

$* bash变量不提供此信息.

The $* bash variable don't give me this info.

推荐答案

您应该能够使用-t测试开关来判断输出流是否是tty:

You should be able to use the -t test switch to tell if the output streams are ttys or not:

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

-t 0用于stdin.

-t 2用于stderr.

这篇关于在bash脚本中测试stdout和stderr重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆