#!/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 stack_lines=("${BASH_LINENO[@]}") stack_functions=("${FUNCNAME[@]}") stack_sources=("${BASH_SOURCE[@]}") echo "${stack_lines[@]}" echo "${stack_functions[@]}" echo "${stack_sources[@]}" 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 exec 3>&- 4>&- builtin exit $code } # MAIN set -TEue -o pipefail trap 'exit $?' ERR TERM INT EXIT # magic FD exec 3>$TMP_ERROR 4>/dev/stderr $DEBUG && >/dev/tty echo '---stdout---' source $SCRIPT 2>&3 success