John Garbutt | daadf74 | 2012-04-27 18:28:28 +0100 | [diff] [blame] | 1 | #!/bin/bash |
2 | |||||
3 | set -e | ||||
4 | set -o xtrace | ||||
5 | |||||
Bob Ball | 5fb83a0 | 2014-07-29 13:40:57 +0100 | [diff] [blame] | 6 | if [ -z "${on_exit_hooks:-}" ]; then |
7 | on_exit_hooks=() | ||||
8 | fi | ||||
John Garbutt | daadf74 | 2012-04-27 18:28:28 +0100 | [diff] [blame] | 9 | |
10 | on_exit() | ||||
11 | { | ||||
Sean Dague | 16dd8b3 | 2014-02-03 09:10:54 +0900 | [diff] [blame] | 12 | for i in $(seq $((${#on_exit_hooks[*]} - 1)) -1 0); do |
John Garbutt | daadf74 | 2012-04-27 18:28:28 +0100 | [diff] [blame] | 13 | eval "${on_exit_hooks[$i]}" |
14 | done | ||||
15 | } | ||||
16 | |||||
17 | add_on_exit() | ||||
18 | { | ||||
19 | local n=${#on_exit_hooks[*]} | ||||
20 | on_exit_hooks[$n]="$*" | ||||
Sean Dague | 16dd8b3 | 2014-02-03 09:10:54 +0900 | [diff] [blame] | 21 | if [[ $n -eq 0 ]]; then |
John Garbutt | daadf74 | 2012-04-27 18:28:28 +0100 | [diff] [blame] | 22 | trap on_exit EXIT |
23 | fi | ||||
24 | } |