-
Notifications
You must be signed in to change notification settings - Fork 3
/
set_if_unset
55 lines (45 loc) · 1.82 KB
/
set_if_unset
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/usr/bin/env /bin/bash
set -e
set_from_var_if_unset() {
# Set the value of var named $1 to the value of var named $2 iff var named $1 does
# not exist. Echo some useful info. If $2 is not given, "DEFAULT_$1" will be used.
# (NOTE: we do NOT set value of $1 if $1 value is "", so that caller can use empty
# value for var named $1).
#
# Examples:
# - "set_from_var_if_unset VAR1" will set VAR1 to the value of var DEFAULT_VAR1
# *only* if VAR1 does not exist; if VAR1 has value "" or something else, it is
# not changed.
# - "set_from_var_if_unset VAR1 VAR2" will set VAR1 to the value of VAR2 under same
# conditions as other examples
#
# WARNING: both args are used in an "eval" so this is not safe to use unless you
# trust where $1 and $2 come from
VAR_NAME="$1"
DEF_VAR_NAME="${2-DEFAULT_$VAR_NAME}"
if [[ "${!VAR_NAME-unset}" == "unset" ]]; then
eval "$VAR_NAME=\"${!DEF_VAR_NAME}\""
echo "WARNING: Using default value for $VAR_NAME"
fi
echo "$VAR_NAME: ${!VAR_NAME}"
}
set_from_value_if_unset() {
# Set the value of var named $1 to the given value iff var named $1 does
# not exist. Echo some useful info.
# (NOTE: we do NOT set value of $1 if $1 value is "", so that caller can use empty
# value for var named $1).
#
# Examples:
# - "set_from_value_if_unset VAR1 value1" will set VAR1 to the value of value1 only
# if VAR1 does not exist; if VAR1 has value "" or something else, it is not changed.
#
# WARNING: both args are used in an "eval" so this is not safe to use unless you
# trust where $1 and $2 come from
VAR_NAME="$1"
VAR_VALUE="$2"
if [[ "${!VAR_NAME-unset}" == "unset" ]]; then
eval "$VAR_NAME=\"${!VAR_VALUE}\""
echo "WARNING: Using default value for $VAR_NAME"
fi
echo "$VAR_NAME: ${!VAR_NAME}"
}