From 1195c9916ed0bcc0ad7cf10fc9d606d9924114b1 Mon Sep 17 00:00:00 2001 From: Chris Koeritz Date: Thu, 21 Jan 2021 16:33:25 +0000 Subject: [PATCH] new script for hiding output on success this script will take a command line to execute and redirect the commands stdout and stderr to two files. if the command does not return an error code, then the output files are just deleted. if there is an error reported, then the two files are sent back to stdout and stderr as they would normally have been, and an additional error message is sent to stderr. --- scripts/testing/squelch_unless_error.sh | 26 +++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 scripts/testing/squelch_unless_error.sh diff --git a/scripts/testing/squelch_unless_error.sh b/scripts/testing/squelch_unless_error.sh new file mode 100644 index 00000000..b59ca5a1 --- /dev/null +++ b/scripts/testing/squelch_unless_error.sh @@ -0,0 +1,26 @@ +#!/bin/bash + +# redirects standard out and standard error output to temp files and runs all the parameters to this script as a command. +# if there is no error, then the files are just deleted. +# if there was an error, then the two output file are sent to standard out and standard error. +# an additional error message is sent to standard error. + +# echo "squelch args: $(printf -- "[%s] " "${@}")" + +newout="$(mktemp /tmp/squelch.out.XXXXXX)" +newerr="$(mktemp /tmp/squelch.err.XXXXXX)" + +eval "${@}" >"$newout" 2>"$newerr" +retval=$? + +if [ $retval != 0 ]; then + # there was an error during the execution of the command. + cat "$newout" + cat "$newerr" >&2 + echo "An error was returned during execution of: ${@}" >&2 +fi + +# clean up. +\rm "$newout" "$newerr" + + -- 2.34.1