-
Notifications
You must be signed in to change notification settings - Fork 0
/
host-env-variables.sh
executable file
·79 lines (70 loc) · 2.64 KB
/
host-env-variables.sh
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/bin/sh
#-------------------------------------------------------------------------------
# Functions to check and create host environment variables.
#-------------------------------------------------------------------------------
#-------------------------------------------------------------------------------
# Adds a "env_keep" statement for a given environment variable. Takes one
# mandatory argument:
#
# 1. "${1:?}" -the name of the environment variable.
#
# N.B.
# This function assumes that the sudoers config file exists.
#-------------------------------------------------------------------------------
addHostEnvVariableToSudoersConf () {
local ENV_VARIABLE="${1:?}"
local ENV_KEEP="Defaults env_keep += \"$ENV_VARIABLE\""
echo "$ENV_KEEP" >> "$SUDOERS_DEFAULT_CONF"
}
#-------------------------------------------------------------------------------
# Checks for a given environment variable in "$PROFILE". Returns true if the
# variable is present, returns false if not. Takes one mandatory argument:
#
# 1. "{1:?}" - the name of the environment variable.
#-------------------------------------------------------------------------------
checkForHostEnvVariable () {
local ENV_VARIABLE="${1:?}"
local ENV_TF="$(grep "$ENV_VARIABLE" "$PROFILE")"
if [ -z "$ENV_TF" ]; then
echo false
else
echo true
fi
}
#-------------------------------------------------------------------------------
# Checks for, then adds, an environment variable to "$PROFILE". Takes two
# mandatory arguments:
#
# 1. "{1:?}" - the name of the environment variable; and
# 2. "{2:?}" - the value of the environment variable.
#
# If the variable is already in "$PROFILE" no changes are made. If the variable
# is not present in "$PROFILE" it is added.
#
# Variables are added as per:
#
# - https://askubuntu.com/a/211718
#
# N.B.
# For the shell to pick this up it requires the user to log out and back in.
#-------------------------------------------------------------------------------
setHostEnvVariable () {
local ENV_VARIABLE="${1:?}"
local ENV_VALUE="${2:?}"
local ENV_TF="$(checkForHostEnvVariable "$ENV_VARIABLE")"
local EXPORT="export $ENV_VARIABLE=$ENV_VALUE"
if [ "$ENV_TF" = true ]; then
echoComment "Already added $ENV_VARIABLE. No changes made."
elif [ "$ENV_TF" = false ]; then
echoComment "Adding $ENV_VARIABLE=$ENV_VALUE to:"
echoComment "$PROFILE"
echo "$EXPORT" >> "$PROFILE"
echoComment 'Checking value added.'
echoSeparator
grep "$ENV_VARIABLE" "$PROFILE"
echoSeparator
echoComment "$ENV_VARIABLE added."
echoSeparator
echoComment 'This variable will not be recognised until you log out and back in.'
fi
}