Browse Source

working FD3+4

main
pvincent 2 weeks ago
parent
commit
fc132ec52c
  1. 3
      _stderr
  2. 3
      _stdout
  3. 7
      bin/bash-strict
  4. 31
      capture.sh
  5. 4
      err
  6. 2
      stderr
  7. 5
      stdout
  8. 4
      test5.sh
  9. 24
      test7.sh
  10. 34
      test8.sh
  11. 10
      useless.sh

3
_stderr

@ -0,0 +1,3 @@
DETECTED0: [This is stderr
./useless.sh: line 9: command_not_found: command not found]
ON_EXIT2 127

3
_stdout

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

7
bin/bash-strict

@ -8,13 +8,14 @@ TMP_ERROR=$(mktemp -t "$(basename $0).XXXXXXXX" --tmpdir=/run/user/$(id -u))
function exit { function exit {
code=${1:-0} code=${1:-0}
echo
echo "ERROR #${code} due to:"
stack_lines=("${BASH_LINENO[@]}") stack_lines=("${BASH_LINENO[@]}")
stack_functions=("${FUNCNAME[@]}") stack_functions=("${FUNCNAME[@]}")
stack_sources=("${BASH_SOURCE[@]}") stack_sources=("${BASH_SOURCE[@]}")
tmp_error=${2:-/dev/null} tmp_error=${2:-/dev/null}
echo "ERROR #${code} due to:"
[[ -s $tmp_error ]] && cat $tmp_error && rm $tmp_error [[ -s $tmp_error ]] && cat $tmp_error && rm $tmp_error
echo "${stack_lines[@]}" echo "${stack_lines[@]}"
@ -39,10 +40,12 @@ for i in "${original_args[@]}"; do quoted_args+=("${i@Q}"); done
set -TEue -o pipefail set -TEue -o pipefail
export -f exit export -f exit
# echo PROCESS START # echo PROCESS START
bash $opt_debug -c " bash $opt_debug -c "
set -TEue -o pipefail; set -TEue -o pipefail;
trap 'exit \$? $TMP_ERROR' ERR TERM INT EXIT; trap 'exit \$? $TMP_ERROR' ERR TERM INT EXIT;
source $command ${quoted_args[*]} source $command ${quoted_args[*]}
" 3>&1 1>&2 2>&3 | tee $TMP_ERROR " 3>&1 1>&2 2>&3 | tee $TMP_ERROR
# echo PROCESS END succesfully
# TODO: delay the stacktrace message to output to stderr, idea: $TMP_ERROR + $TMP_STACK
# echo PROCESS END succesfully

31
capture.sh

@ -0,0 +1,31 @@
#!/bin/bash
{
IFS=$'\n' read -r -d '' CAPTURED_STDERR
IFS=$'\n' read -r -d '' CAPTURED_STDOUT
} < <((printf '\0%s\0' "$(./useless.sh)" 1>&2) 2>&1)
echo 'Here is the captured stdout:'
echo "${CAPTURED_STDOUT}"
echo
echo 'And here is the captured stderr:'
echo "${CAPTURED_STDERR}"
echo
# function launch {
# echo START
# bash -c "set -TEue -o pipefail; . ./useless.sh"
# echo END
# }
#
# set -TEue -o pipefail
# stderr=$(launch 3>&1 1>&2 2>&3 | tee /dev/stderr)
# >&2 echo "ERR: $stderr"
# # (
# echo -n "ERR: "
# cat stderr
# ) >&2
# >&2 echo 'here is the captured stderr:'
# >&2 echo "$(<stderr)"
# >&2 echo

4
err

@ -0,0 +1,4 @@
This is stderr
./useless.sh: line 6: command_not_found: command not found
DETECTED [This is stderr
./useless.sh: line 6: command_not_found: command not found]

2
stderr

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

5
stdout

@ -0,0 +1,5 @@
stdout A
stdout B
DETECTED3 [This is stderr
./useless.sh: line 6: nope: unbound variable]
ON_EXIT 1

4
test5.sh

@ -1,10 +1,10 @@
#!/usr/bin/env -S -- bash-strict
#!./bin/bash-strict
function f2 { function f2 {
# return 6 # return 6
# exit 7 # exit 7
unknown_command
echo $nope echo $nope
unknown_command
} }
function f1 { function f1 {

24
test7.sh

@ -0,0 +1,24 @@
#!/bin/bash
# function launch {
# exec 3>&1 1>&2 2>&3 ./useless.sh
# exec 3>&1 1>&2 2>&3
# }
function on_exit {
FAILURE=${1:-0}
[[ $FAILURE -gt 0 ]] && >&2 echo EXIT "$FAILURE"
trap - ERR TERM INT EXIT
}
export -f on_exit
{
myvar=$(bash -c "
set -TEue -o pipefail;
trap 'on_exit \$?' ERR TERM INT EXIT
. ./useless.sh
" 3>&1 1>&2 2>&3 | tee /dev/fd/4 | 3>&5 1>&2 2>&3 tee /dev/fd/5)
} 5>&1 4>&2 # magic trick: fd4 grabs stderr, then show up again on stderr
echo ----------
echo -e "stderr is:\n$myvar"

34
test8.sh

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

10
useless.sh

@ -0,0 +1,10 @@
#!/bin/bash
echo "stdout A"
# echo toto >>/tmp/toto
>&2 echo "This is stderr"
echo "stdout B"
# $nope
echo "stdout C"
command_not_found
echo END of useless
Loading…
Cancel
Save