I need to check if my program's output is being redirected; if yes I need to keep and send its by mail.
example:
$ 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
You can check if stdout is a terminal. When stdout is redirected or piped it will not be a terminal. You can use the test command with the -t
option to get this information:
if [ -t 1 ] ; then
# stdout is a terminal
else
# stdout isn't a terminal
fi
From man test
:
-t FD file descriptor FD is opened on a terminal
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With