2537 |
02 Jul 14 |
nicklas |
#!/bin/sh |
2537 |
02 Jul 14 |
nicklas |
# $Id$ |
2537 |
02 Jul 14 |
nicklas |
3 |
# |
2537 |
02 Jul 14 |
nicklas |
# Wrapper script to be used for "misbehaving" programs that |
2537 |
02 Jul 14 |
nicklas |
# write everyting to stderr, including important output and |
2537 |
02 Jul 14 |
nicklas |
# error messages. |
2537 |
02 Jul 14 |
nicklas |
7 |
# |
2537 |
02 Jul 14 |
nicklas |
# The wrapper calls the program and redirect the output to |
2537 |
02 Jul 14 |
nicklas |
# stdout if the program exits with 0 or to stderr if it |
2537 |
02 Jul 14 |
nicklas |
# exits with some other value. |
2537 |
02 Jul 14 |
nicklas |
11 |
# |
2537 |
02 Jul 14 |
nicklas |
# Usage: ./stdwrap cmd [other parameters] |
2537 |
02 Jul 14 |
nicklas |
13 |
# |
2537 |
02 Jul 14 |
nicklas |
14 |
|
2537 |
02 Jul 14 |
nicklas |
# create temporary file in current directory |
2537 |
02 Jul 14 |
nicklas |
16 |
TMP=`mktemp --tmpdir=.` |
2537 |
02 Jul 14 |
nicklas |
17 |
|
2537 |
02 Jul 14 |
nicklas |
# start program and redirect stderr + stdout to the temporary file |
2537 |
02 Jul 14 |
nicklas |
19 |
CMD=$1 |
2537 |
02 Jul 14 |
nicklas |
20 |
shift |
2537 |
02 Jul 14 |
nicklas |
21 |
${CMD} "${@}" 2> ${TMP} 1>&2 |
2537 |
02 Jul 14 |
nicklas |
22 |
|
2537 |
02 Jul 14 |
nicklas |
# get exit code |
2537 |
02 Jul 14 |
nicklas |
24 |
EXIT_CODE=$? |
2537 |
02 Jul 14 |
nicklas |
25 |
|
2537 |
02 Jul 14 |
nicklas |
26 |
if [ ${EXIT_CODE} -eq 0 ]; then |
2537 |
02 Jul 14 |
nicklas |
# exit code is 0, copy temporary file to stdout |
2537 |
02 Jul 14 |
nicklas |
28 |
cat ${TMP} |
2537 |
02 Jul 14 |
nicklas |
29 |
rm ${TMP} |
2537 |
02 Jul 14 |
nicklas |
30 |
else |
2537 |
02 Jul 14 |
nicklas |
# exit code is != 0, copy temporary file to stderr |
2537 |
02 Jul 14 |
nicklas |
32 |
cat ${TMP} 1>&2 |
2537 |
02 Jul 14 |
nicklas |
33 |
rm ${TMP} |
2537 |
02 Jul 14 |
nicklas |
# exit with same exit code |
2537 |
02 Jul 14 |
nicklas |
35 |
exit ${EXIT_CODE} |
2537 |
02 Jul 14 |
nicklas |
36 |
fi |
2537 |
02 Jul 14 |
nicklas |
37 |
|