From 121531f5ea6d4969ff0bb1964bd046f2c4efe2e0 Mon Sep 17 00:00:00 2001 From: pvincent Date: Thu, 9 Jul 2026 23:17:49 +0400 Subject: [PATCH] DEBUG on tty --- _stderr | 3 --- _stdout | 2 -- bin/bash-strict2.sh | 52 +++++++++++++++++++++++++++++++++++++++++++++ stderr | 2 -- useless.sh | 5 +++-- 5 files changed, 55 insertions(+), 9 deletions(-) delete mode 100644 _stderr delete mode 100644 _stdout create mode 100755 bin/bash-strict2.sh delete mode 100644 stderr diff --git a/_stderr b/_stderr deleted file mode 100644 index fe6a27b..0000000 --- a/_stderr +++ /dev/null @@ -1,3 +0,0 @@ -DETECTED0: [This is stderr -./useless.sh: line 7: nope: unbound variable] -EXIT 1 diff --git a/_stdout b/_stdout deleted file mode 100644 index 9c5a3b7..0000000 --- a/_stdout +++ /dev/null @@ -1,2 +0,0 @@ -stdout A -stdout B diff --git a/bin/bash-strict2.sh b/bin/bash-strict2.sh new file mode 100755 index 0000000..c3d4d8c --- /dev/null +++ b/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 diff --git a/stderr b/stderr deleted file mode 100644 index 839e3d7..0000000 --- a/stderr +++ /dev/null @@ -1,2 +0,0 @@ -This is stderr -./useless.sh: line 7: nope: unbound variable diff --git a/useless.sh b/useless.sh index c3e38e6..b788a1c 100755 --- a/useless.sh +++ b/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