Provide do_on_exit() function

This function allows to register an exit action which executes when the
calling script terminates.
This commit is contained in:
Reto Buerki 2012-12-06 19:17:30 +01:00 committed by Tobias Brunner
parent 7c2ef58e86
commit 9a045eef8e
1 changed files with 28 additions and 0 deletions

View File

@ -86,6 +86,34 @@ log_status()
echo
}
# the following two functions are stolen from [1]
# [1] - http://www.linuxjournal.com/content/use-bash-trap-statement-cleanup-temporary-files
declare -a on_exit_items
# perform registered actions on exit
on_exit()
{
for i in "${on_exit_items[@]}"
do
eval $i >>$LOGFILE 2>&1
done
on_exit_items=""
trap - EXIT
}
# register a command to execute when the calling script terminates. The
# registered commands are called in FIFO order.
# $* - command to register
do_on_exit()
{
local n=${#on_exit_items[*]}
on_exit_items[$n]="$*"
if [ $n -eq 0 ]; then
trap on_exit EXIT
fi
}
#############################################
# search and replace strings throughout a
# whole directory