Mercurial > hg > config
comparison bin/simpleenv.sh @ 861:c3a87bd40482
simple environment
author | Jeff Hammel <k0scist@gmail.com> |
---|---|
date | Wed, 18 Apr 2018 10:31:25 -0700 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
860:af998083be84 | 861:c3a87bd40482 |
---|---|
1 # source it | |
2 | |
3 export_var() { | |
4 if [[ "$#" != "2" ]] | |
5 then | |
6 echo "Must be called with two arguments" | |
7 return 1 | |
8 fi | |
9 # https://serverfault.com/questions/7503/how-to-determine-if-a-bash-variable-is-empty/382740#382740 | |
10 local KEY=$1 | |
11 local VALUE="${!KEY}" | |
12 if [[ -n "${!KEY+set}" ]]; | |
13 then | |
14 _OLD_VARS+=( "${KEY}" ) | |
15 _OLD_VALS+=( "${VALUE}" ) | |
16 else | |
17 _OLD_UNSET+=( "${KEY}" ) | |
18 fi | |
19 export "${1}=${2}" | |
20 } | |
21 | |
22 export_var BROWSER firefox | |
23 | |
24 deactivate() { | |
25 local index=0 | |
26 for key in "${_OLD_VARS[@]}" | |
27 do | |
28 local value="${_OLD_VALS[$index]}" | |
29 export "$key=$value" | |
30 index=$((index+1)) | |
31 done | |
32 for var in "${_OLD_UNSET[@]}" | |
33 do | |
34 unset "${var}" | |
35 done | |
36 unset deactivate | |
37 } |