I wrote a shell script that I use under ash, and I redirect stderr and stdout to a log file. I would like that log file to be emailed to me only if stderr is not empty.
I tried:
exec >mylog.log 2>&1
# Perform various find commands
if [TEST_IF_STDERR_NOT_EMPTY]; then
/usr/bin/mail -s "mylog" [email protected] < mylog.log
fi
My question is twofold:
1- I get a -sh: /usr/bin/mail: not found
error. It seems that the mail command doesn't exist under ash (or at least under my linux box, which is a Synology NAS), what would be the alternative? Worst case, perl is available, but I would prefer to use standard sh commands.
2- How to I test that stderr is not empty?
Thanks
How to check if file is empty in bash
As for the first question, in your code you are calling
mail
but lower in the post you are callingemail
. Check your code and make sure it ismail
.Use
which mail
to get the full path. Maybe it is not installed in /usr/bin/. Use find to locatemail
. If you can go to another shell, run it and then executewhich mail
to get the full path of mail in case the path is set up in the alternative shells.