-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvariable-replace.sh
executable file
·84 lines (67 loc) · 3.37 KB
/
variable-replace.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
80
81
82
83
84
#!/usr/bin/env bash
# -------------------------------------------------------------------------------- #
# Description #
# -------------------------------------------------------------------------------- #
# Replace template variables in a file with values from an associative array. #
# -------------------------------------------------------------------------------- #
# -------------------------------------------------------------------------------- #
# Ref Array #
# -------------------------------------------------------------------------------- #
# Take the name of an array and create a vaid reference to it. #
# -------------------------------------------------------------------------------- #
function ref_array()
{
local varname="$1"
local export_as="$2"
local code
code=$(declare -p "$varname")
echo "${code/$varname/$export_as}"
}
# -------------------------------------------------------------------------------- #
# Replace Variables #
# -------------------------------------------------------------------------------- #
# Take the name of an array and create a vaid reference to it. #
# -------------------------------------------------------------------------------- #
function replace_variables()
{
eval "$(ref_array "$1" array)"
filename="${2}"
local i
local search
local replace
# shellcheck disable=SC2154
for i in "${!array[@]}"; do
search="${i}"
replace="${array[$i]}"
if grep -q "${search}" "${filename}"; then
sed -i '' "s/${search}/${replace}/g" "${filename}"
printf "Replaced %s with %s\n" "${search}" "${replace}"
fi
done
}
# -------------------------------------------------------------------------------- #
# Run Tests #
# -------------------------------------------------------------------------------- #
# A VERY simple test function to ensure that it all works #
# -------------------------------------------------------------------------------- #
function run_tests()
{
declare -A config
# shellcheck disable=SC2034
config=(
['{{ USERNAME_PLACE_HOLDER }}']='admin'
['{{ PASSWORD_PLACE_HOLDER }}']='password'
)
replace_variables "config" "test.sh"
}
# -------------------------------------------------------------------------------- #
# Main() #
# -------------------------------------------------------------------------------- #
# This is the actual 'script' and the functions/sub routines are called in order. #
# -------------------------------------------------------------------------------- #
run_tests "$@"
# -------------------------------------------------------------------------------- #
# End of Script #
# -------------------------------------------------------------------------------- #
# This is the end - nothing more to see here. #
# -------------------------------------------------------------------------------- #