What's the best way to suppress output (stdout and stderr) unless the program exits with a non-zero code? I'm thinking:
quiet_success()
{
file=/tmp/suppressed
if ! ( "$@" > "$file" 2>&1 ); then
cat "$file"
fi
}
And run quiet_success my_long_noisy_script.sh
but I'm not sure if there's a better way. I feel like this has to be something other people have needed to do.
For the record, I'm looking to add this to my cron scripts, so that I get emailed with everything if they fail, but not if they don't.
sh
syntax, and your redirection order is backwards (do> "$file" 2>&1
and use more quotes). – jw013 Jun 22 '12 at 14:50