changeset 861:c3a87bd40482

simple environment
author Jeff Hammel <k0scist@gmail.com>
date Wed, 18 Apr 2018 10:31:25 -0700
parents af998083be84
children e300310bb98c
files bin/simpleenv.sh
diffstat 1 files changed, 37 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/bin/simpleenv.sh	Wed Apr 18 10:31:25 2018 -0700
@@ -0,0 +1,37 @@
+# source it
+
+export_var() {
+    if [[ "$#" != "2" ]]
+    then
+        echo "Must be called with two arguments"
+        return 1
+    fi
+    # https://serverfault.com/questions/7503/how-to-determine-if-a-bash-variable-is-empty/382740#382740
+    local KEY=$1
+    local VALUE="${!KEY}"
+    if [[ -n "${!KEY+set}" ]];
+    then
+        _OLD_VARS+=( "${KEY}" )
+        _OLD_VALS+=( "${VALUE}" )
+    else
+        _OLD_UNSET+=( "${KEY}" )
+    fi
+    export "${1}=${2}"
+}
+
+export_var BROWSER firefox
+
+deactivate() {
+    local index=0
+    for key in "${_OLD_VARS[@]}"
+    do
+        local value="${_OLD_VALS[$index]}"
+        export "$key=$value"
+        index=$((index+1))
+    done
+    for var in "${_OLD_UNSET[@]}"
+    do
+        unset "${var}"
+    done
+    unset deactivate
+}