我需要检查我的程序的输出是否被重定向; 如果是的话我需要保留并通过邮件发送.
例:
$ myprogram -param1 -param2 -param3 > /home/polly/log.txt
Run Code Online (Sandbox Code Playgroud)
myprogram.sh:
if 'redirection is not empty'; then
cat <redirection name> | mailx -s "This is a test email." polly@gmail.com
fi
Run Code Online (Sandbox Code Playgroud)
hek*_*mgl 12
您可以检查stdout是否是终端.当stdout被重定向或管道传输时,它将不是终端.您可以使用带有-t选项的test命令来获取此信息:
if [ -t 1 ] ; then
# stdout is a terminal
else
# stdout isn't a terminal
fi
Run Code Online (Sandbox Code Playgroud)
来自man test:
Run Code Online (Sandbox Code Playgroud)-t FD file descriptor FD is opened on a terminal