fixed comments
[feisty_meow.git] / scripts / testing / squelch_unless_error.sh
1 #!/bin/bash
2
3 # redirects standard out and standard error output to temp files and runs all
4 # the parameters to this script as a command.
5 # if there is no error, then the files are just deleted.
6 # if there was an error, then the two output files are sent to standard out
7 # and standard error, and an additional explanatory error message is sent to
8 # standard error about the command that failed.
9
10 #echo "squelch args: $(printf -- "[%s] " "${@}")"
11
12 newout="$(mktemp /tmp/squelch.out.XXXXXX)"
13 newerr="$(mktemp /tmp/squelch.err.XXXXXX)"
14
15 eval "${@}" >"$newout" 2>"$newerr"
16 retval=$?
17
18 if [ $retval != 0 ]; then
19   # there was an error during the execution of the command.
20   cat "$newout"
21   cat "$newerr" >&2
22   echo "An error was returned during execution of: ${@}" >&2
23 fi
24
25 # clean up.
26 \rm "$newout" "$newerr"
27
28 # pass along the error code we saw, whether success or failure, so that this command has same
29 # exit result as the original would have.
30 exit $retval
31