From 9871f5d3ca1984495f53a94ad415cb41b9949c7a Mon Sep 17 00:00:00 2001 From: pvincent Date: Thu, 9 Jul 2026 14:24:38 +0400 Subject: [PATCH] clean up --- _stderr | 4 ++-- _stdout | 1 - stderr | 2 +- test8.sh | 30 +++++++++++------------------- useless.sh | 4 ++-- 5 files changed, 16 insertions(+), 25 deletions(-) diff --git a/_stderr b/_stderr index 5a88af6..fe6a27b 100644 --- a/_stderr +++ b/_stderr @@ -1,3 +1,3 @@ DETECTED0: [This is stderr -./useless.sh: line 9: command_not_found: command not found] -ON_EXIT2 127 +./useless.sh: line 7: nope: unbound variable] +EXIT 1 diff --git a/_stdout b/_stdout index 2720e46..9c5a3b7 100644 --- a/_stdout +++ b/_stdout @@ -1,3 +1,2 @@ stdout A stdout B -stdout C diff --git a/stderr b/stderr index 0fdf0d9..839e3d7 100644 --- a/stderr +++ b/stderr @@ -1,2 +1,2 @@ This is stderr -./useless.sh: line 9: command_not_found: command not found +./useless.sh: line 7: nope: unbound variable diff --git a/test8.sh b/test8.sh index 268aa44..179a189 100755 --- a/test8.sh +++ b/test8.sh @@ -1,34 +1,26 @@ #!/bin/bash -function on_exit { +function exit { code=${1:-0} - # exec 3>&1 1>&2 2>&3 - # exec 1>&- 3>&- 2>&- - # exec 2> >(tee /dev/stderr) trap - ERR TERM INT EXIT - # exec 2>&- - # exec 1> >(tee -ap /dev/stdout) 2>&- 3> >(tee -ap /dev/stderr) - # exec 2>&- 3>&- - # echo CLOSE OPEN PIPE suite + + # a bit of magic: read content of file still on write! count=$(wc -c stderr | cut -d' ' -f1) content=$(head -qc$count stderr) >&4 echo "DETECTED0: [$content]" - >&4 echo ON_EXIT2 ${code} + + >&4 echo EXIT ${code} } set -TEue -o pipefail -trap 'on_exit $?' ERR TERM INT EXIT -# exec 2>stderr 3> >(tee -ap /dev/stderr) +trap 'exit $?' ERR TERM INT EXIT [[ -f stderr ]] && rm stderr -# exec 1> >(tee -p /dev/stdout) 2> >(tee stderr | tee -p /dev/stderr) 3> >(tee -p /dev/stderr) -# exec 2> >(tee stderr | tee -p /dev/stderr) # 3> >(tee -p /dev/stderr) -# exec 3> >(tee stderr | tee -p /dev/stderr) 2>&3 -# exec 3> >(tee stderr) 2>&3- -# exec 2> >(tee stderr) +export -f exit + +# magic FD exec 3>stderr 4>/dev/stderr source ./useless.sh 2>&3 exec 3>&- 4>&- ->&2 cat stderr + trap - ERR TERM INT EXIT -echo --END1 ->&2 echo --END2 +[[ -s stderr ]] && >&2 cat stderr diff --git a/useless.sh b/useless.sh index 5ea6cac..c3e38e6 100755 --- a/useless.sh +++ b/useless.sh @@ -4,7 +4,7 @@ echo "stdout A" # echo toto >>/tmp/toto >&2 echo "This is stderr" echo "stdout B" -# $nope +$nope echo "stdout C" -command_not_found +# command_not_found echo END of useless