-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbash-lib-properties
executable file
·71 lines (66 loc) · 1.8 KB
/
bash-lib-properties
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
#!/bin/bash
#
# Bash library to load Java properties file into BASH variables
#
# NOTE:
# AWK code was copied from Stackoverflow
# https://stackoverflow.com/questions/1682442/reading-java-properties-file-from-bash
#
# Dependency: bash-lib-logging
#
source lib/bash-lib-logging
readonly AWK_CODE__READ_PROPERTIES_FILE='
BEGIN {
FS="=";
print "# BEGIN";
n="";
v="";
c=0; # Not a line continuation.
}
/^#/ { # The line is a comment. Breaks line continuation.
c=0;
next;
}
/\\$/ && (c==0) && (NF>=2) { # Name value pair with a line continuation...
e=index($0,"=");
n=substr($0,1,e-1);
v=substr($0,e+1,length($0) - e - 1); # Trim off the backslash.
c=1; # Line continuation mode.
next;
}
/^[^\\]+\\$/ && (c==1) { # Line continuation. Accumulate the value.
v= "" v substr($0,1,length($0)-1);
next;
}
((c==1) || (NF>=2)) && !/^[^\\]+\\$/ { # End of line continuation, or a single line name/value pair
if (c==0) { # Single line name/value pair
e=index($0,"=");
n=substr($0,1,e-1);
v=substr($0,e+1,length($0) - e);
} else { # Line continuation mode - last line of the value.
c=0; # Turn off line continuation mode.
v= "" v $0;
}
# Make sure the name is a legal shell variable name
gsub(/[^A-Za-z0-9_]/,"_",n);
# Remove newlines from the value.
gsub(/[\n\r]/,"",v);
print n "=\"" v "\"";
n = "";
v = "";
}
END {
print "# END";
}'
loadPropertiesFile()
{
local _PROPERTIES_FILE=$1
if [[ -z "${_PROPERTIES_FILE}" ]]; then
abort "Properties file name not provided"
fi
if [[ ! -f "${_PROPERTIES_FILE}" ]]; then
abort "Properties file [$_PROPERTIES_FILE] doesn't exist."
fi
source <(cat "${_PROPERTIES_FILE}" | awk "${AWK_CODE__READ_PROPERTIES_FILE}")
}
#::END::