@@ -193,14 +193,40 @@ function process_value()
193
193
value=" ${value%% \# * } " # Remove in line right comments
194
194
value=" ${value##* ( )} " # Remove leading spaces
195
195
value=" ${value%%* ( )} " # Remove trailing spaces
196
- value=" ${value# \" * } " # Remove leading string quotes
197
- value=" ${value% \" * } " # Remove trailing string quotes
198
- value=" ${value##* ( )} " # Remove leading spaces
199
- value=" ${value%%* ( )} " # Remove trailing spaces
196
+
197
+ value=$( escape_string " $value " )
200
198
201
199
echo " ${value} "
202
200
}
203
201
202
+ # -------------------------------------------------------------------------------- #
203
+ # Escape string #
204
+ # -------------------------------------------------------------------------------- #
205
+ # Replace ' with SINGLE_QUOTE to avoid issues with eval. #
206
+ # -------------------------------------------------------------------------------- #
207
+
208
+ function escape_string()
209
+ {
210
+ local clean
211
+
212
+ clean=${1// \' / SINGLE_QUOTE}
213
+ echo " ${clean} "
214
+ }
215
+
216
+ # -------------------------------------------------------------------------------- #
217
+ # Un-Escape string #
218
+ # -------------------------------------------------------------------------------- #
219
+ # Convert SINGLE_QUOTE back to ' when returning the value to the caller. #
220
+ # -------------------------------------------------------------------------------- #
221
+
222
+ function unescape_string()
223
+ {
224
+ local orig
225
+
226
+ orig=${1// SINGLE_QUOTE/ \' }
227
+ echo " ${orig} "
228
+ }
229
+
204
230
# -------------------------------------------------------------------------------- #
205
231
# Parse ini file #
206
232
# -------------------------------------------------------------------------------- #
@@ -220,19 +246,19 @@ function process_ini_file()
220
246
while read -r line; do
221
247
line_number=$(( line_number+ 1 ))
222
248
223
- if [[ $line =~ ^# || -z $line ]]; then # Ignore comments / empty lines
249
+ if [[ $line =~ ^# || -z $line ]]; then # Ignore comments / empty lines
224
250
continue ;
225
251
fi
226
252
227
- if [[ $line =~ ^" [" (.+)" ]" $ ]]; then # Match pattern for a 'section'
253
+ if [[ $line =~ ^" [" (.+)" ]" $ ]]; then # Match pattern for a 'section'
228
254
section=$( process_section_name " ${BASH_REMATCH[1]} " )
229
255
230
256
if ! in_array sections " ${section} " ; then
231
257
eval " ${section} _keys=()" # Use eval to declare the keys array
232
258
eval " ${section} _values=()" # Use eval to declare the values array
233
- sections+=(" $section " ) # Add the section name to the list
259
+ sections+=(" ${ section} " ) # Add the section name to the list
234
260
fi
235
- elif [[ $line =~ ^(.* )" =" (.* ) ]]; then # Match patter for a key=value pair
261
+ elif [[ $line =~ ^(.* )" =" (.* ) ]]; then # Match patter for a key=value pair
236
262
key=$( process_key_name " ${BASH_REMATCH[1]} " )
237
263
value=$( process_value " ${BASH_REMATCH[2]} " )
238
264
@@ -280,7 +306,8 @@ function get_value()
280
306
281
307
for i in " ${! keys[@]} " ; do
282
308
if [[ " ${keys[$i]} " = " ${key} " ]]; then
283
- printf ' %s' " ${values[$i]} "
309
+ orig=$( unescape_string " ${values[$i]} " )
310
+ printf ' %s' " ${orig} "
284
311
fi
285
312
done
286
313
}
@@ -308,7 +335,8 @@ function display_config()
308
335
eval " values=( \"\$ {${section} _values[@]}\" )"
309
336
310
337
for i in " ${! keys[@]} " ; do
311
- printf ' %s=%s\n' " ${keys[$i]} " " ${values[$i]} "
338
+ orig=$( unescape_string " ${values[$i]} " )
339
+ printf ' %s=%s\n' " ${keys[$i]} " " ${orig} "
312
340
done
313
341
printf ' \n'
314
342
done
@@ -336,7 +364,8 @@ function display_config_by_section()
336
364
eval " values=( \"\$ {${section} _values[@]}\" )"
337
365
338
366
for i in " ${! keys[@]} " ; do
339
- printf ' %s=%s\n' " ${keys[$i]} " " ${values[$i]} "
367
+ orig=$( unescape_string " ${values[$i]} " )
368
+ printf ' %s=%s\n' " ${keys[$i]} " " ${orig} "
340
369
done
341
370
printf ' \n'
342
371
}
0 commit comments