Browse Source

clean up

main
pvincent 2 weeks ago
parent
commit
9871f5d3ca
  1. 4
      _stderr
  2. 1
      _stdout
  3. 2
      stderr
  4. 30
      test8.sh
  5. 4
      useless.sh

4
_stderr

@ -1,3 +1,3 @@
DETECTED0: [This is stderr 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

1
_stdout

@ -1,3 +1,2 @@
stdout A stdout A
stdout B stdout B
stdout C

2
stderr

@ -1,2 +1,2 @@
This is stderr This is stderr
./useless.sh: line 9: command_not_found: command not found
./useless.sh: line 7: nope: unbound variable

30
test8.sh

@ -1,34 +1,26 @@
#!/bin/bash #!/bin/bash
function on_exit {
function exit {
code=${1:-0} code=${1:-0}
# exec 3>&1 1>&2 2>&3
# exec 1>&- 3>&- 2>&-
# exec 2> >(tee /dev/stderr)
trap - ERR TERM INT EXIT 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) count=$(wc -c stderr | cut -d' ' -f1)
content=$(head -qc$count stderr) content=$(head -qc$count stderr)
>&4 echo "DETECTED0: [$content]" >&4 echo "DETECTED0: [$content]"
>&4 echo ON_EXIT2 ${code}
>&4 echo EXIT ${code}
} }
set -TEue -o pipefail 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 [[ -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 exec 3>stderr 4>/dev/stderr
source ./useless.sh 2>&3 source ./useless.sh 2>&3
exec 3>&- 4>&- exec 3>&- 4>&-
>&2 cat stderr
trap - ERR TERM INT EXIT trap - ERR TERM INT EXIT
echo --END1
>&2 echo --END2
[[ -s stderr ]] && >&2 cat stderr

4
useless.sh

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