multiple bash traps for the same signal

后端 未结 12 801
悲哀的现实
悲哀的现实 2020-12-13 01:47

When I use the trap command in bash, the previous trap for the given signal is replaced.

Is there a way of making more than one trap<

12条回答
  •  星月不相逢
    2020-12-13 02:42

    Technically you can't set multiple traps for the same signal, but you can add to an existing trap:

    1. Fetch the existing trap code using trap -p
    2. Add your command, separated by a semicolon or newline
    3. 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\n' "$*"; }
    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\n' "$3"; }
                # print existing trap command with newline
                eval "extract_trap_cmd $(trap -p "${trap_add_name}")"
                # print the new trap command
                printf '%s\n' "${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
    

提交回复
热议问题