How can I rewrite this command to only email if there is output from the
mailq | grep?
mailq | egrep 'rejected|refused' -A 5 -B 5 | mail -s 'dd' email@email
Is this even possible on one line?
I think you will have to use a temp file for this operation so that you can use the
If you didn't mind the temp file sticking around somewhere and can use a static name for it, you could skip the special naming and deletion stuff:
Edit: After seeing glenn's answer I played with this some more and apparently assigning a variable using the
Edit 2: After seeing Simon's answer I checked out my
Making this possible:
You could save the output in a variable, and then invoke the mail command if the length of the string is non-zero.
For one line, join the two commands with a semi-colon.
The utility program ifne exists expressly for this purpose: to run a command if the standard input is not empty.
The ifne command is part of the moreutils package, billed as "a growing collection of Unix tools that nobody thought to write long ago, when Unix was young."
On my Mac, I ran the following test. Note that the file
This sends an email to me:
This does not. Note that the command
In this particular case, if your
Note the useful use of
With most shells (anything other than zsh, as far as I know), if the file begins with a null character, this code will believe it's empty. Fixing that is left as an exercise for the reader. (Hint: use