Ich habe hier folgenden Code:
Als Output erhalte ich für die Fälle 1-3
Wie verhindere ich im Fall 2&3, dass die Trap mehrfach aufgerufen wird?
Bash:
#!/bin/bash
set -Eeuo pipefail
shopt -s inherit_errexit
trap 'catch' ERR
function catch () {
echo "Trapped in Subshell #$BASH_SUBSHELL" >&2
echo "BASH_LINENO: ${BASH_LINENO[@]}" >&2
echo "BASH_SOURCE: ${BASH_SOURCE[@]}" >&2
echo "FUNCNAME: ${FUNCNAME[@]}" >&2
echo "------------------------" >&2
}
function sub_test () {
bla
echo "Should not print"
}
function main_test() {
sub_test
echo "Should not print as well"
}
echo "Choose case 1-3"
read c
case "$c" in
1) main_test
;;
2) J=$(main_test)
;;
3) J=$($($($(main_test))))
;;
*) echo "unknown_case!"
;;
esac
echo "Should also not print"
Als Output erhalte ich für die Fälle 1-3
Code:
$ bash ssftest.bash
Choose case 1-3
1
ssftest.bash: line 17: bla: command not found
Trapped in Subshell #0
BASH_LINENO: 17 22 29 0
BASH_SOURCE: ssftest.bash ssftest.bash ssftest.bash ssftest.bash
FUNCNAME: catch sub_test main_test main
------------------------
Code:
$ bash ssftest.bash
Choose case 1-3
2
ssftest.bash: line 17: bla: command not found
Trapped in Subshell #1
BASH_LINENO: 17 22 31 0
BASH_SOURCE: ssftest.bash ssftest.bash ssftest.bash ssftest.bash
FUNCNAME: catch sub_test main_test main
------------------------
Trapped in Subshell #0
BASH_LINENO: 31 0
BASH_SOURCE: ssftest.bash ssftest.bash
FUNCNAME: catch main
------------------------
Code:
$ bash ssftest.bash
Choose case 1-3
3
ssftest.bash: line 17: bla: command not found
Trapped in Subshell #4
BASH_LINENO: 17 22 33 0
BASH_SOURCE: ssftest.bash ssftest.bash ssftest.bash ssftest.bash
FUNCNAME: catch sub_test main_test main
------------------------
Trapped in Subshell #3
BASH_LINENO: 33 0
BASH_SOURCE: ssftest.bash ssftest.bash
FUNCNAME: catch main
------------------------
Trapped in Subshell #2
BASH_LINENO: 33 0
BASH_SOURCE: ssftest.bash ssftest.bash
FUNCNAME: catch main
------------------------
Trapped in Subshell #1
BASH_LINENO: 33 0
BASH_SOURCE: ssftest.bash ssftest.bash
FUNCNAME: catch main
------------------------
Trapped in Subshell #0
BASH_LINENO: 33 0
BASH_SOURCE: ssftest.bash ssftest.bash
FUNCNAME: catch main
------------------------
Wie verhindere ich im Fall 2&3, dass die Trap mehrfach aufgerufen wird?
- Die 'set' und 'shopt' Optionen sollten beibehalten werden.
- Einen Umweg über temporäre Dateien würd ich gern vermeiden.