-
Notifications
You must be signed in to change notification settings - Fork 0
/
supuner
executable file
·126 lines (110 loc) · 3.73 KB
/
supuner
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#! /bin/sh
# Copyright (c)2011 Laurence Tratt <http://tratt.net/laurie>
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.
# http://tratt.net/laurie/src/supuner
#
# supuner [-e] [-o <filename>] <command>
#
# This script takes a command and executes it, manipulating its stderr and
# stdout. Its name is short for SUPress UNless ERror (supuner).
#
# By default sunuper suppresses all output to stderr and stdout unless an
# error occurs in which case a combined copy of stderr and stdout is sent
# to stderr. With the -e ('echo') switch, the command's normal stdout and
# stderr are sent to the console. This allows one to both interactively
# view and log a commands output simultaneously
#
# If -o is specified, the captured stderr / stdout is combined and sent to
# that file.
#
# The exit code of supuner is the exit code of <command>.
#
# This is useful for "chatty" scripts used in cron jobs which output
# irrelevant information on stderr, which clogs up cron's output. This
# script allows one to only see the errors such scripts cause.
prog_name=$(basename "$0")
ec=0 # Set to 1 for 'echo to stdout'
op=0 # output path
dop=1 # Delete output path when script finishes
ae=0 # Set to != 0 to indicate an error with the programs arguments
while getopts ":ho:e" f
do
case "${f}" in
e) ec=1;;
o) op="${OPTARG}"; dop=0;;
[?]) ae=1; break;;
esac
done
if [ "${ae}" -eq 0 ]; then
shift $((OPTIND-1))
if [ $# -eq 0 ]; then
ae=1
else
if echo "$1" | grep "^\-" > /dev/null 2> /dev/null; then
ae=1
else
prog=$1
shift
fi
fi
fi
if [ "${ec}" -eq 1 ] && [ "${op}" = 0 ]; then
echo "-e only allowed if -o is specified" >&2
exit 1
fi
if [ "${ae}" -ne 0 ]; then
echo "Usage: ${prog_name} [-e] [-o <filename>] <command>" >&2
exit 1
fi
tmpdir=$(mktemp -d)
trap 'rm -fr ${tmpdir}' HUP INT QUIT PIPE TERM
if [ "${dop}" -eq 1 ]; then
# If the user didn't specify an op file, we use a temporary one.
op=${tmpdir}/op
fi
if [ "${ec}" -eq 1 ]; then
# We have to make sure the commands output is sent to the real
# stderr / stdout as well as captured. This code is inspired by
# http://www.travishartwell.net/blog/2006/08/19_2220
p=${tmpdir}/p
mkfifo "${p}"
exec 3>&1 4>&2
tee "${op}" < "${p}" >&3 &
tpid=$!
exec > "${p}" 2>&1
${prog} ${1+"$@"}
rc=$?
exec 1>&3 3>&- 2>&4 4>&-
wait "${tpid}"
rm -f "${p}"
else
# Simple: execute the command and only spit something to stdout if a
# non-zero exit code is returned.
exec 3>&1 4>&2
exec > "${op}" 2>&1
${prog} ${1+"$@"}
rc=$?
exec 1>&3 3>&- 2>&4 4>&-
if [ "${rc}" -ne 0 ]; then
cat "${op}" >&2
fi
fi
rm -fr "${tmpdir}"
exit "${rc}"