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