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

More refactoring of suppress command

parent 400eadb6
...@@ -95,15 +95,32 @@ function gh-clone { ...@@ -95,15 +95,32 @@ 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 {
# https://unix.stackexchange.com/questions/256120/how-can-i-suppress-output-only-if-the-command-succeeds#256122 # Run a command, show output only if return code not 0.
tmp=$(mktemp) || return # Takes into account state of -e option.
echo "Running $@" # Compare
"$@" > "$tmp" 2>&1 # https://unix.stackexchange.com/questions/256120/how-can-i-suppress-output-only-if-the-command-succeeds#256122
ret=$? # Set -e stuff agonized over in
[ "$ret" -eq 0 ] || cat "$tmp" # https://unix.stackexchange.com/questions/296526/set-e-in-a-subshell
rm -f "$tmp" local tmp=$(mktemp tmp.XXXXXXXXX) || return
return "$ret" local opts=$-
echo "Running $@"
set +e
( set_opts $opts ; $@ > "$tmp" 2>&1 ) ; ret=$?
[ "$ret" -eq 0 ] || cat "$tmp"
rm -f "$tmp"
set_opts $opts
return "$ret"
} }
function rm_mkdir { function rm_mkdir {
......
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