3 # a framework for stacking EXIT traps
4 # this could, should be further enhanced to allow stacks of traps for various
5 # exits. but right now it's hard-coded for EXIT traps
11 local trap_handle="$2"
13 local var="exit_trap_handle_$trap_handle"
15 if [ -n "${!var}" ]; then
16 echo "fail! trap handle $trap_handle is already in use"
20 local num_items=${#exit_actions[@]}
21 exit_actions[$num_items]="$action"
22 eval $var="$num_items"
29 local trap_handle="$1"
31 local var="exit_trap_handle_$trap_handle"
32 local trap_num=${!var}
33 exit_actions[$trap_num]=""
39 local i num_items=${#exit_actions[@]}
40 for i in $(seq 0 $((num_items-1))); do
41 if [ -z "${exit_actions[$i]}" ]; then
44 echo "${exit_actions[$i]}"
51 local i num_items=${#exit_actions[@]}
52 for i in $(seq $((num_items-1)) -1 0); do
53 if [ -z "${exit_actions[$i]}" ]; then
56 eval ${exit_actions[$i]}
61 trap run_exit_traps EXIT
63 #if ! push_exit_trap "echo \"this is the first trap\"" "a"; then
64 # echo "failed to install trap 1"
67 #if ! push_exit_trap "echo \"this is the second trap\"" "b"; then
68 # echo "failed to install trap 2"
72 #if ! push_exit_trap "echo \"this is the third trap\"" "b"; then
73 # echo "failed to install trap 3"