1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
|
#!/bin/bash
# use "quiet foo" when you expect "foo" to produce a lot of output
# that isn't useful unless foo itself fails.
quiet() (
# note this is a subshell (parens instead of braces around the function)
# so this set only affects this function and not the caller
{ set +x; } >&/dev/null
# not strictly needed because it's a subshell, but good practice
local tf retval
tf="$(mktemp)"
set +e
"$@" >& "$tf"
retval=$?
set -e
if [ "$retval" != "0" ]; then
echo "quiet: $*" >&2
echo "quiet: exit status $retval. Output follows:" >&2
cat "$tf" >&2
echo "quiet: end of output." >&2
fi
rm -f -- "$tf"
return $retval
)
quiet "$@"
|