blob: a4db39c22565ec2e539387051fee35fb79ce752d [file] [log] [blame]
John Garbuttdaadf742012-04-27 18:28:28 +01001#!/bin/bash
2
3set -e
4set -o xtrace
5
6declare -a on_exit_hooks
7
8on_exit()
9{
10 for i in $(seq $((${#on_exit_hooks[*]} - 1)) -1 0)
11 do
12 eval "${on_exit_hooks[$i]}"
13 done
14}
15
16add_on_exit()
17{
18 local n=${#on_exit_hooks[*]}
19 on_exit_hooks[$n]="$*"
20 if [[ $n -eq 0 ]]
21 then
22 trap on_exit EXIT
23 fi
24}