25 lines
355 B
Bash
Raw Normal View History

#!/bin/bash
set -e
set -o xtrace
if [ -z "${on_exit_hooks:-}" ]; then
on_exit_hooks=()
fi
on_exit()
{
for i in $(seq $((${#on_exit_hooks[*]} - 1)) -1 0); do
eval "${on_exit_hooks[$i]}"
done
}
add_on_exit()
{
local n=${#on_exit_hooks[*]}
on_exit_hooks[$n]="$*"
if [[ $n -eq 0 ]]; then
trap on_exit EXIT
fi
}