blob: 90696a2d426ff013d1aaec7800c6dc880bc2a00c [file] [log] [blame]
#!/bin/bash
# Provides an "atexit" mechanism for scripts.
# Array of commands to eval.
declare -a __atexit_cmds
# Helper for eval'ing commands.
__atexit() {
for cmd in "${__atexit_cmds[@]}"; do
eval ${cmd}
done
}
# Usage: atexit command arg1 arg2 arg3
atexit() {
# Determine the current number of commands.
local length=${#__atexit_cmds[*]}
# Add this command to the end.
__atexit_cmds[${length}]="${*}"
# Set the trap handler if this was the first command added.
if [[ ${length} -eq 0 ]]; then
trap __atexit EXIT
fi
}