Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
fengzch-das
multibuild
Commits
c909ecb2
Commit
c909ecb2
authored
May 22, 2018
by
Matthew Brett
Browse files
More refactoring of suppress command
parent
400eadb6
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
25 additions
and
8 deletions
+25
-8
common_utils.sh
common_utils.sh
+25
-8
No files found.
common_utils.sh
View file @
c909ecb2
...
@@ -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
"
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment