Technically you can't set multiple traps for the same signal, but you can add to an existing trap:
- Fetch the existing trap code using
trap -p
- Add your command, separated by a semicolon or newline
- Set the trap to the result of #2
Here is a bash function that does the above:
# note: printf is used instead of echo to avoid backslash
# processing and to properly handle values that begin with a '-'.
log() { printf '%s
' "$*"; }
error() { log "ERROR: $*" >&2; }
fatal() { error "$@"; exit 1; }
# appends a command to a trap
#
# - 1st arg: code to add
# - remaining args: names of traps to modify
#
trap_add() {
trap_add_cmd=$1; shift || fatal "${FUNCNAME} usage error"
for trap_add_name in "$@"; do
trap -- "$(
# helper fn to get existing trap command from output
# of trap -p
extract_trap_cmd() { printf '%s
' "$3"; }
# print existing trap command with newline
eval "extract_trap_cmd $(trap -p "${trap_add_name}")"
# print the new trap command
printf '%s
' "${trap_add_cmd}"
)" "${trap_add_name}"
|| fatal "unable to add to trap ${trap_add_name}"
done
}
# set the trace attribute for the above function. this is
# required to modify DEBUG or RETURN traps because functions don't
# inherit them unless the trace attribute is set
declare -f -t trap_add
Example usage:
trap_add 'echo "in trap DEBUG"' DEBUG
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…