Browse Source

DEBUG on tty

main
pvincent 2 weeks ago
parent
commit
121531f5ea
  1. 3
      _stderr
  2. 2
      _stdout
  3. 52
      bin/bash-strict2.sh
  4. 2
      stderr
  5. 5
      useless.sh

3
_stderr

@ -1,3 +0,0 @@
DETECTED0: [This is stderr
./useless.sh: line 7: nope: unbound variable]
EXIT 1

2
_stdout

@ -1,2 +0,0 @@
stdout A
stdout B

52
bin/bash-strict2.sh

@ -0,0 +1,52 @@
#!/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

2
stderr

@ -1,2 +0,0 @@
This is stderr
./useless.sh: line 7: nope: unbound variable

5
useless.sh

@ -2,9 +2,10 @@
echo "stdout A"
# echo toto >>/tmp/toto
# $nope
# exit 6
>&2 echo "This is stderr"
echo "stdout B"
$nope
echo "stdout C"
# command_not_found
command_not_found
echo END of useless
Loading…
Cancel
Save