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_handles="$@"
32 for handle in $trap_handles; do
33 local var="exit_trap_handle_$handle"
34 local trap_num=${!var}
35 exit_actions[$trap_num]=""
42 local i num_items=${#exit_actions[@]}
43 for i in $(seq 0 $((num_items-1))); do
44 if [ -z "${exit_actions[$i]}" ]; then
47 echo "${exit_actions[$i]}"
54 local i num_items=${#exit_actions[@]}
55 for i in $(seq $((num_items-1)) -1 0); do
56 if [ -z "${exit_actions[$i]}" ]; then
59 eval ${exit_actions[$i]}
64 trap run_exit_traps EXIT
66 if [ "$1" = "unit_test" ]; then
67 if ! push_exit_trap "echo \"this is the first trap\"" "a"; then
68 echo "failed to install trap 1"
71 if ! push_exit_trap "echo \"this is the second trap\"" "b"; then
72 echo "failed to install trap 2"
76 if ! push_exit_trap "echo \"this is the third trap\"" "b"; then
77 echo "failed to install trap 3"
85 delete_exit_trap "a" "b"
89 if ! push_exit_trap "echo \"this is the first trap\"" "a"; then
90 echo "failed to install trap 1"
93 if ! push_exit_trap "echo \"this is the second trap\"" "b"; then
94 echo "failed to install trap 2"
97 if ! push_exit_trap "echo \"this is the third trap\"" "c"; then
98 echo "failed to install trap 3"
101 delete_exit_trap "a" "c"