Commit c909ecb2 authored by Matthew Brett's avatar Matthew Brett
Browse files

More refactoring of suppress command

parent 400eadb6
...@@ -95,14 +95,31 @@ function gh-clone { ...@@ -95,14 +95,31 @@ function gh-clone {
git clone https://github.com/$1 git clone https://github.com/$1
} }
function set_opts {
# Set options from input options string (in $- format).
local opts=$1
local chars="exhimBH"
for (( i=0; i<${#chars}; i++ )); do
char=${chars:$i:1}
[ -n "${opts//[^${char}]/}" ] && set -$char || set +$char
done
}
function suppress { function suppress {
# Run a command, show output only if return code not 0.
# Takes into account state of -e option.
# Compare
# https://unix.stackexchange.com/questions/256120/how-can-i-suppress-output-only-if-the-command-succeeds#256122 # https://unix.stackexchange.com/questions/256120/how-can-i-suppress-output-only-if-the-command-succeeds#256122
tmp=$(mktemp) || return # Set -e stuff agonized over in
# https://unix.stackexchange.com/questions/296526/set-e-in-a-subshell
local tmp=$(mktemp tmp.XXXXXXXXX) || return
local opts=$-
echo "Running $@" echo "Running $@"
"$@" > "$tmp" 2>&1 set +e
ret=$? ( set_opts $opts ; $@ > "$tmp" 2>&1 ) ; ret=$?
[ "$ret" -eq 0 ] || cat "$tmp" [ "$ret" -eq 0 ] || cat "$tmp"
rm -f "$tmp" rm -f "$tmp"
set_opts $opts
return "$ret" return "$ret"
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment