You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
52 lines
966 B
52 lines
966 B
#!/bin/bash
|
|
|
|
# CONSTANTS
|
|
DEBUG=true
|
|
TMP_ERROR=$(mktemp -t "$(basename $0).XXXXXXXX" --tmpdir=/run/user/$(id -u))
|
|
SCRIPT=$1
|
|
|
|
# FUNCTIONS
|
|
|
|
function exit {
|
|
code=${1:-0}
|
|
|
|
# a bit of magic: read content of file still on write!
|
|
# count=$(wc -c $TMP_ERROR | cut -d' ' -f1)
|
|
# content=$(head -qc$count $TMP_ERROR)
|
|
|
|
if [[ -s $TMP_ERROR ]]; then
|
|
content=$(<$TMP_ERROR)
|
|
$DEBUG && >/dev/tty echo '---stderr---'
|
|
>&4 echo "DETECTED: [$content]"
|
|
fi
|
|
cleanup ${code}
|
|
}
|
|
|
|
function success {
|
|
if [[ -s $TMP_ERROR ]]; then
|
|
$DEBUG && >/dev/tty echo '---stderr---'
|
|
>&2 cat $TMP_ERROR
|
|
fi
|
|
cleanup 0
|
|
}
|
|
|
|
function cleanup {
|
|
code=${1:-0}
|
|
$DEBUG && >/dev/tty echo "---exit $1---"
|
|
trap - ERR TERM INT EXIT
|
|
rm $TMP_ERROR
|
|
builtin exit $code
|
|
}
|
|
|
|
# MAIN
|
|
|
|
set -TEue -o pipefail
|
|
trap 'exit $?' ERR TERM INT EXIT
|
|
# export -f exit
|
|
|
|
# magic FD
|
|
exec 3>$TMP_ERROR 4>/dev/stderr
|
|
$DEBUG && >/dev/tty echo '---stdout---'
|
|
source $SCRIPT 2>&3
|
|
exec 3>&- 4>&-
|
|
success
|