annotate bin/simpleenv.sh @ 903:08da6a1bb4c9

py3
author Jeff Hammel <k0scist@gmail.com>
date Sat, 18 Nov 2023 13:24:26 -0800
parents c3a87bd40482
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
861
c3a87bd40482 simple environment
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
1 # source it
c3a87bd40482 simple environment
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
2
c3a87bd40482 simple environment
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
3 export_var() {
c3a87bd40482 simple environment
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
4 if [[ "$#" != "2" ]]
c3a87bd40482 simple environment
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
5 then
c3a87bd40482 simple environment
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
6 echo "Must be called with two arguments"
c3a87bd40482 simple environment
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
7 return 1
c3a87bd40482 simple environment
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
8 fi
c3a87bd40482 simple environment
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
9 # https://serverfault.com/questions/7503/how-to-determine-if-a-bash-variable-is-empty/382740#382740
c3a87bd40482 simple environment
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
10 local KEY=$1
c3a87bd40482 simple environment
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
11 local VALUE="${!KEY}"
c3a87bd40482 simple environment
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
12 if [[ -n "${!KEY+set}" ]];
c3a87bd40482 simple environment
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
13 then
c3a87bd40482 simple environment
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
14 _OLD_VARS+=( "${KEY}" )
c3a87bd40482 simple environment
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
15 _OLD_VALS+=( "${VALUE}" )
c3a87bd40482 simple environment
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
16 else
c3a87bd40482 simple environment
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
17 _OLD_UNSET+=( "${KEY}" )
c3a87bd40482 simple environment
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
18 fi
c3a87bd40482 simple environment
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
19 export "${1}=${2}"
c3a87bd40482 simple environment
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
20 }
c3a87bd40482 simple environment
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
21
c3a87bd40482 simple environment
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
22 export_var BROWSER firefox
c3a87bd40482 simple environment
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
23
c3a87bd40482 simple environment
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
24 deactivate() {
c3a87bd40482 simple environment
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
25 local index=0
c3a87bd40482 simple environment
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
26 for key in "${_OLD_VARS[@]}"
c3a87bd40482 simple environment
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
27 do
c3a87bd40482 simple environment
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
28 local value="${_OLD_VALS[$index]}"
c3a87bd40482 simple environment
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
29 export "$key=$value"
c3a87bd40482 simple environment
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
30 index=$((index+1))
c3a87bd40482 simple environment
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
31 done
c3a87bd40482 simple environment
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
32 for var in "${_OLD_UNSET[@]}"
c3a87bd40482 simple environment
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
33 do
c3a87bd40482 simple environment
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
34 unset "${var}"
c3a87bd40482 simple environment
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
35 done
c3a87bd40482 simple environment
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
36 unset deactivate
c3a87bd40482 simple environment
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
37 }