From 36ec21abc93958e1c5f37b88c8b5d16807437170 Mon Sep 17 00:00:00 2001 From: Nathaniel Landau Date: Fri, 23 Jul 2021 13:31:13 -0400 Subject: [PATCH] add _safeExit_ --- utilities/baseHelpers.bash | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/utilities/baseHelpers.bash b/utilities/baseHelpers.bash index 7c2ea9d..f080550 100644 --- a/utilities/baseHelpers.bash +++ b/utilities/baseHelpers.bash @@ -333,3 +333,29 @@ _setPATH_() { fi done } + +_safeExit_() { + # DESC: Cleanup and exit from a script + # ARGS: $1 (optional) - Exit code (defaults to 0) + # OUTS: None + + if [[ -d "${script_lock-}" ]]; then + if command rm -rf "${script_lock}"; then + debug "Removing script lock" + else + warning "Script lock could not be removed. Try manually deleting ${tan}'${lock_dir}'${red}" + fi + fi + + if [[ -n "${tmpDir-}" && -d "${tmpDir-}" ]]; then + if [[ ${1-} == 1 && -n "$(ls "${tmpDir}")" ]]; then + command rm -r "${tmpDir}" + else + command rm -r "${tmpDir}" + debug "Removing temp directory" + fi + fi + + trap - INT TERM EXIT + exit ${1:-0} +}