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