devstack/tools/xen/scripts/on_exit.sh
Bob Ball 5fb83a0a33 XenAPI: Fix declare definition
Some versions of bash do not initialise a variable with declare -a
therefore with set -u bash can terminate the script.

Be more verbose in declaring the array if it is not set

Change-Id: I6ec2b6e986aeffe539a2ab93432fa7af9e5a4f5d
2014-07-29 13:40:57 +01:00

25 lines
355 B
Bash
Executable File

#!/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
}