github.com/openflowlabs/storage@v1.12.13/contrib/cirrus/ooe.sh (about) 1 #!/bin/bash 2 3 # This script executes a command while logging all output to a temporary 4 # file. If the command exits non-zero, then all output is sent to the console, 5 # before returning the exit code. If the script itself fails, the exit code 121 6 # is returned. 7 8 set -eo pipefail 9 10 SCRIPT_BASEDIR="$(basename $0)" 11 12 badusage() { 13 echo "Incorrect usage: $SCRIPT_BASEDIR) <command> [options]" > /dev/stderr 14 echo "ERROR: $1" 15 exit 121 16 } 17 18 COMMAND="$@" 19 [[ -n "$COMMAND" ]] || badusage "No command specified" 20 21 OUTPUT_TMPFILE="$(mktemp -p '' ${SCRIPT_BASEDIR}_output_XXXX)" 22 output_on_error() { 23 RET=$? 24 set +e 25 if [[ "$RET" -ne "0" ]] 26 then 27 echo "---------------------------" 28 cat "$OUTPUT_TMPFILE" 29 echo "[$(date --iso-8601=second)] <exit $RET> $COMMAND" 30 fi 31 rm -f "$OUTPUT_TMPFILE" 32 } 33 trap "output_on_error" EXIT 34 35 "$@" 2>&1 | while IFS='' read LINE # Preserve leading/trailing whitespace 36 do 37 # Every stdout and (copied) stderr line 38 echo "[$(date --iso-8601=second)] $LINE" 39 done >> "$OUTPUT_TMPFILE"