5698 |
04 Nov 19 |
nicklas |
#!/bin/sh |
5698 |
04 Nov 19 |
nicklas |
# $Id $ |
5698 |
04 Nov 19 |
nicklas |
3 |
# |
5698 |
04 Nov 19 |
nicklas |
# Wrapper script to be used when the output to stderr |
5698 |
04 Nov 19 |
nicklas |
# should be re-directed to different places depending on |
5698 |
04 Nov 19 |
nicklas |
# the exit status of the executed command. |
5698 |
04 Nov 19 |
nicklas |
7 |
# |
5698 |
04 Nov 19 |
nicklas |
# The wrapper calls the program and if the exit status |
5698 |
04 Nov 19 |
nicklas |
# is non-zero, stderr is redirected to stderr as usual |
5698 |
04 Nov 19 |
nicklas |
# otherwise stderr is redirected to fd3. |
5698 |
04 Nov 19 |
nicklas |
11 |
# |
5698 |
04 Nov 19 |
nicklas |
# Usage: ./stderrwrap cmd [other parameters] 2> stderr-fail.txt 3> stderr-ok.txt |
5698 |
04 Nov 19 |
nicklas |
13 |
# |
5698 |
04 Nov 19 |
nicklas |
14 |
|
5698 |
04 Nov 19 |
nicklas |
# create temporary file in current directory |
5698 |
04 Nov 19 |
nicklas |
16 |
TMP=`mktemp --tmpdir=.` |
5698 |
04 Nov 19 |
nicklas |
17 |
|
5698 |
04 Nov 19 |
nicklas |
# start program and redirect stderr to the temporary file |
5698 |
04 Nov 19 |
nicklas |
19 |
CMD=$1 |
5698 |
04 Nov 19 |
nicklas |
20 |
shift |
5698 |
04 Nov 19 |
nicklas |
21 |
${CMD} "${@}" 2> ${TMP} |
5698 |
04 Nov 19 |
nicklas |
22 |
|
5698 |
04 Nov 19 |
nicklas |
# get exit code |
5698 |
04 Nov 19 |
nicklas |
24 |
EXIT_CODE=$? |
5698 |
04 Nov 19 |
nicklas |
25 |
|
5698 |
04 Nov 19 |
nicklas |
26 |
if [ ${EXIT_CODE} -eq 0 ]; then |
5698 |
04 Nov 19 |
nicklas |
# exit code is 0, copy temporary file to fd3 |
5698 |
04 Nov 19 |
nicklas |
28 |
cat ${TMP} 1>&3 |
5698 |
04 Nov 19 |
nicklas |
29 |
rm ${TMP} |
5698 |
04 Nov 19 |
nicklas |
30 |
else |
5698 |
04 Nov 19 |
nicklas |
# exit code is != 0, copy temporary file to stderr |
5698 |
04 Nov 19 |
nicklas |
32 |
cat ${TMP} 1>&2 |
5698 |
04 Nov 19 |
nicklas |
33 |
rm ${TMP} |
5698 |
04 Nov 19 |
nicklas |
# exit with same exit code |
5698 |
04 Nov 19 |
nicklas |
35 |
exit ${EXIT_CODE} |
5698 |
04 Nov 19 |
nicklas |
36 |
fi |
5698 |
04 Nov 19 |
nicklas |
37 |
|