-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathini_parser.sh
executable file
·263 lines (241 loc) · 5.61 KB
/
ini_parser.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#!/bin/bash
#
# Introduction
# ------------
#
# Functions for parsing INI-style file natively in bash.
#
# The INI file format supports the following features:
#
# Sections: [section]
# Properties: name=value
# Comments: ; comment
# # comment
#
# Blank lines and trailing writespace are ignored as is whitespace around
# the '=' - ie.
#
# name = value
#
# is equivalent to
#
# name=value
#
# Whitespace and quotes within a value are preserved (though values don't
# in general need to be quoted and will not be subject to shell parameter
# splitting)
#
# Values can be continuted onto subsequent lines if these are prefixed with
# whitespace - ie.
#
# name=line1
# line2
#
# is equivalent to:
#
# name = line1 line2
#
# Properties are stored in the global _CONFIG array as ( k1 v1 k2 v2 ... ) with
# keys in the format "<section>.<name>" (properties without an associated
# section are stored as ".<name>". In most cases this is transparent as the
# list/get commands can be used to query the data
#
# The functionality is more or less equivalent to the Python ConfigParser
# module with the following exceptions
#
# - Properties are allowed outside sections
# - Multi-line properties are joined with ' ' rather than '\n' (due to shell
# quoting issues)
#
# Usage
# -----
#
# Given the following ini file (test.ini) -
#
# ; A test ini file
# global = a global value
# [section1]
# abc = def ; a comment
# ghi = jkl
# [section2]
# xyz = abc ; extends over two lines
# def
#
# Parse config file -
#
# $ parseIniFile < test/t2.ini
#
# $ listKeys
# .global
# section1.abc
# section1.ghi
# section2.xyz
#
# $ listAll
# .global a global value
# section1.abc def
# section1.ghi jkl
# section2.xyz abc def
#
# $ listSection section1
# section1.abc def
# section1.ghi jkl
#
# $ getProperty global
# a global value
#
# $ getProperty section2.xyz
# abc def
#
# $ getPropertyVar XYZ section2.xyz && echo OK
# OK
#
# $ echo ">${XYZ}<"
# >abc def<
#
shopt -s extglob
_CONFIG=()
# Commands
# --------
#
# parseIniFile < file
#
# Parse ini file (reads from stdin) and saves data to global _CONFIG var
#
function parseIniFile() {
local LINE=""
local SECTION=""
local KEY=""
local VALUE=""
local IFS=""
while read LINE
do
LINE=${LINE%%[;#]*} # Strip comments
LINE=${LINE%%*( )} # Strip trailing whitespace
if [[ -n $KEY && $LINE =~ ^[[:space:]]+(.+) ]] # Continuation - append value
then
VALUE+=" ${BASH_REMATCH[1]}"
else
if [[ -n $KEY ]] # No continuation
then
_CONFIG=(${_CONFIG[@]} "${SECTION}.${KEY}" "${VALUE}")
KEY=""
VALUE=""
fi
if [[ $LINE =~ ^\[([[:alnum:]]+)\] ]] # Section
then
SECTION=${BASH_REMATCH[1]}
KEY=""
elif [[ $LINE =~ ^([^[:space:]]+)[[:space:]]*=[[:space:]]*(.+) ]] # Property
then
KEY=${BASH_REMATCH[1]}
VALUE="${BASH_REMATCH[2]}"
fi
fi
done
if [[ -n $KEY ]]
then
_CONFIG=(${_CONFIG[@]} "${SECTION}.${KEY}" "${VALUE}")
fi
}
# listKeys
#
# List keys present in config file in format "<section>.<property>"
#
function listKeys() {
local -i i
for ((i=0; i<${#_CONFIG[@]}; i+=2))
do
echo ${_CONFIG[i]}
done
}
# listAll
#
# List keys and data in format "<section>.<property> <value>"
#
function listAll() {
local -i i
for ((i=0; i<${#_CONFIG[@]}; i+=2))
do
echo ${_CONFIG[i]} ${_CONFIG[((i+1))]}
done
}
# listSection <section>
#
# List keys and data for given section (sepcified as $1) in format
# "<property> <value>"
#
function listSection() {
local -i i
local SECTION=$1
for ((i=0; i<${#_CONFIG[@]}; i+=2))
do
if [[ ${_CONFIG[$i]} =~ ^${SECTION}\.(.+) ]]
then
echo ${_CONFIG[i]} ${_CONFIG[((i+1))]}
fi
done
}
# getProperty [name|section.name]
#
# Print value for given property (sepcified as $1)
# Properties without a section can be queried directly as
# "name" (rather than ".name")
#
# Returns 0 (true) if property found otherwise 1 (false)
#
function getProperty() {
local -i i
local KEY=$1
for ((i=0; i<${#_CONFIG[@]}; i+=2))
do
if [[ ${_CONFIG[$i]} =~ ^\.?${KEY} ]]
then
echo ${_CONFIG[((i+1))]}
return 0
fi
done
return 1
}
# getPropertyVar <variable> [name|section.name]
#
# Save value for given property (sepcified as $2)
# into shell variable (specified as $1)
# Properties without a section can be queried directly as
# "name" (rather than ".name")
#
# Returns 0 (true) if property found otherwise 1 (false)
#
function getPropertyVar() {
local -i i
local VAR=$1
local KEY=$2
for ((i=0; i<${#_CONFIG[@]}; i+=2))
do
if [[ ${_CONFIG[$i]} =~ ^\.?${KEY} ]]
then
local VAL=${_CONFIG[((i+1))]}
eval ${VAR}="\${VAL}"
return 0
fi
done
return 1
}
function testIniParser() {
echo $ parseIniFile \< test/t2.ini
parseIniFile < test/t2.ini
echo $ listKeys
listKeys
echo $ listAll
listAll
echo $ listSection section1
listSection section1
echo $ getProperty global
getProperty global
echo $ getProperty section2.xyz
getProperty section2.xyz
echo $ getPropertyVar XYZ section2.xyz \&\& echo OK
getPropertyVar XYZ section2.xyz && echo OK
echo $ echo ">\${XYZ}<"
echo ">${XYZ}<"
}