我需要检查程序的输出是否被重定向;如果是,我需要保留并通过邮件发送。
例子:
$ myprogram -param1 -param2 -param3 > /home/polly/log.txt
myprogram.sh
:
if 'redirection is not empty'; then
cat <redirection name> | mailx -s "This is a test email." [email protected]
fi
您可以检查stdout是否为终端。当stdout重定向或通过管道传输时,它将不是终端。您可以将test命令与-t
选项一起使用以获取此信息:
if [ -t 1 ] ; then
# stdout is a terminal
else
# stdout isn't a terminal
fi
来自man test
:
-t FD file descriptor FD is opened on a terminal
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句