-
Notifications
You must be signed in to change notification settings - Fork 13
/
quietrun
executable file
·77 lines (67 loc) · 2.17 KB
/
quietrun
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/bin/sh
#
# quietrun -- run a command and only allow output if the exit code
# didn't reflect success.
# Copyright (c) 1999 Adam Spiers <adam@spiers.net>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# See also chronic from moreutils which does the same thing without
# temporary files:
# http://git.kitenet.net/?p=moreutils.git;a=blob;f=chronic;hb=HEAD
tmpdir=/tmp/quietrun.$$
# Half-heartedly guard against symlink attacks/accidents. There's
# still a race. Hmm. Does anyone actually care?
[ -e $tmpdir ] && rm -rf $tmpdir
if [ -e $tmpdir ]; then
echo "Couldn't remove $tmpdir; aborting."
exit 1
fi
if ! mkdir $tmpdir; then
echo "Couldn't create $tmpdir; aborting."
exit 1
fi
if [ "$1" = '-h' ] || [ "$1" = '--help' ]; then
cat <<'EOF'
Usage: quietrun [ -m | --merge ] <command> [ <args> ... ]
If the merge option is specified, stdout and stderr are merged into
stdout, but the output order is preserved. Otherwise they kept
separate, but output from stderr only appears after output from stdout.
The exit code from the command is preserved.
EOF
exit 0
fi
if [ "$1" = '-m' ] || [ "$1" = '--merge' ]; then
shift
merge=yes
fi
if [ $# -eq 0 ]; then
echo "quietrun: no command supplied"
exit 1
fi
if [ "$merge" = 'yes' ]; then
"$@" 2>&1 >$tmpdir/both
exit_code=$?
if [ $exit_code != 0 ]; then
[ -e "$tmpdir/both" ] && cat $tmpdir/both
fi
else
"$@" >$tmpdir/stdout 2>$tmpdir/stderr
exit_code=$?
if [ $exit_code != 0 ]; then
[ -e "$tmpdir/stdout" ] && cat $tmpdir/stdout
[ -e "$tmpdir/stderr" ] && cat $tmpdir/stderr 1>&2 2>/dev/null
fi
fi
rm -rf $tmpdir
exit $exit_code