summaryrefslogtreecommitdiffstats
path: root/tests/traps.rc
blob: f011960c97e628adfb279b4bbe208c37950cbf33 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#!/bin/bash

# Make sure this only gets included/executed once.  Unfortunately, bash doesn't
# usually distinguish between values that are unset and values that are null.
# To work around that, we declare TRAPFUNCS to be a one-element array right at
# the start, but that one element is : which is defined to do nothing.

if [ ${#TRAPFUNCS[@]} = 0 ]; then
        TRAPFUNCS=(:)

        push_trapfunc () {
                TRAPFUNCS[${#TRAPFUNCS[@]}]="$@"
        }

        execute_trapfuncs () {
                for i in "${TRAPFUNCS[@]}"; do
                        $i
                done
        }

        trap execute_trapfuncs EXIT
fi