This question already has an answer here:
I'm trying to modify a script which emails a user group the contents of a directory. The modification I'm looking for is to check the script below to see if it has valid output before sending.
The script is:
cd $foo/bar && ls | mail -s "Filenames in the \$foo/bar Directory" firstname.lastname@example.org
I want to test if the file is null before sending this email, as this will go on three dozen servers and many to the same person/people, so I don't want to send a bunch of blank emails (which is the status quo).
I have looked at other similar questions, but none of them quite scratch the itch. This one is closest:
but is for a file attached, which won't work. Also,
man mail returns a
-E flag that is supposed to perform the content check, but I've played with it and my version doesn't seem to support the
-E option (which is not something that can readily be changed). I get an error
mail: invalid option -- E.
Any help would be appreciated. It doesn't have to be a one liner, but that sure would help!
EDIT - I need the directories to be dismissed in the output check, if this is possible! I did not clarify this originally, but if $foo/bar has a directory of
/errors can this be avoided?