From: Reto Buerki Date: Thu, 6 Dec 2012 18:17:30 +0000 (+0100) Subject: Provide do_on_exit() function X-Git-Tag: 5.0.2rc1~1^2~79 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=9a045eef8e003b46fb93369a7888d65b01730a1d;p=thirdparty%2Fstrongswan.git Provide do_on_exit() function This function allows to register an exit action which executes when the calling script terminates. --- diff --git a/testing/scripts/function.sh b/testing/scripts/function.sh index 3e0560a46c..d9f2054efc 100755 --- a/testing/scripts/function.sh +++ b/testing/scripts/function.sh @@ -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