From 01f998e9eeab6c3409ab7cc8dbb67a5b414ea89c Mon Sep 17 00:00:00 2001 From: phenonymous <29523105+phenonymous@users.noreply.github.com> Date: Fri, 30 Nov 2018 23:03:39 +0100 Subject: [PATCH] Make sure exported variables get unset to avoid global variable pollution --- apply-colors.sh | 33 ++++++++++++++++++++++++++++++++- gogh.sh | 18 ++++++++++++++---- 2 files changed, 46 insertions(+), 5 deletions(-) diff --git a/apply-colors.sh b/apply-colors.sh index fdd130a..cc17c9f 100644 --- a/apply-colors.sh +++ b/apply-colors.sh @@ -10,6 +10,35 @@ GS="${GS:-$(command -v gsettings | xargs echo)}" # Note: xargs echo is to make the command sucessful even if it was not # otherwise the script will exit if the command does not exist (elementary os) +# | +# | Make sure all exported variables get unset no matter what +# | Defining this in this script because it gets called even if +# | gogh.sh was not called. Exported variables in gogh.sh gets +# | handled there in case there was en error before this script was called +# | ============================================ +GLOBAL_VAR_CLEANUP() { + unset PROFILE_NAME + unset PROFILE_SLUG + unset scratchdir + unset TILIX_RES + unset TERMINAL + unset LOOP + unset OPTLENGTH + + for c in $(seq -s " " -w 16); do + unset DEMO_COLOR_${c} + unset COLOR_${c} + done + + unset BACKGROUND_COLOR + unset FOREGROUND_COLOR + unset CURSOR_COLOR + unset PROFILE_NAME +} + +# Note: Since all scripts gets invoked in a subshell the traps from the parent shell +# will not get inherited. Hence traps defined in gogh.sh and print-themes.sh will still trigger +trap 'GLOBAL_VAR_CLEANUP; trap - EXIT' EXIT HUP INT QUIT PIPE TERM # | # | Second test for TERMINAL in case user ran @@ -252,6 +281,7 @@ if [[ "${COLORTERM:-}" == "truecolor" ]] || [[ "${COLORTERM:-}" == "24bit" ]]; t [[ "$c" == "08" ]] && color_str+="\n" # new line done printf '\n%b\n\n\n' "${color_str}" + unset color_str } else function gogh_colors () { @@ -262,6 +292,7 @@ else [[ $c == 7 ]] && color_str+="\n" # new line done printf '\n%b\n\n' "${color_str}" + unset color_str } fi @@ -594,4 +625,4 @@ esac unset PROFILE_NAME unset PROFILE_SLUG -unset DEFAULT_SLUG \ No newline at end of file +unset DEFAULT_SLUG diff --git a/gogh.sh b/gogh.sh index 657ffc7..a6b3cf0 100755 --- a/gogh.sh +++ b/gogh.sh @@ -1,5 +1,16 @@ #!/usr/bin/env bash +# Define traps and trapfunctions early in case any errors before script exits +GLOBAL_VAR_CLEANUP(){ + [[ -n "$(command -v TILIX_TMP_CLEANUP)" ]] && TILIX_TMP_CLEANUP + unset PROFILE_NAME + unset PROFILE_SLUG + unset TILIX_RES + unset TERMINAL +} + +trap 'GLOBAL_VAR_CLEANUP; trap - EXIT' EXIT HUP INT QUIT PIPE TERM + declare -a THEMES=( '3024-day.sh' '3024-night.sh' @@ -341,18 +352,17 @@ if [[ "$TERMINAL" = "tilix" ]] && [[ ${#OPTION[@]} -gt 0 ]]; then # | desides to abort before all themes has been processed this section will cleanup the tmpdir # | ======================================= if [[ ${TILIX_RES::1} =~ ^(y|Y)$ ]]; then - cleanup() { + TILIX_TMP_CLEANUP() { echo echo "Cleaning up" rm -rf "$scratchdir" - unset LOOP OPTLENGTH + unset LOOP OPTLENGTH scratchdir echo "Done..." exit 0 } scratchdir=$(mktemp -d -t tmp.XXXXXXXX) export scratchdir - trap 'cleanup; trap - EXIT' EXIT HUP INT QUIT PIPE TERM fi fi @@ -400,4 +410,4 @@ for OP in "${OPTION[@]#0}"; do exit 1 fi done -command -v bar::stop > /dev/null && bar::stop || : \ No newline at end of file +command -v bar::stop > /dev/null && bar::stop || :