@@ -193,14 +193,40 @@ function process_value()
193193 value=" ${value%% \# * } " # Remove in line right comments
194194 value=" ${value##* ( )} " # Remove leading spaces
195195 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 " )
200198
201199 echo " ${value} "
202200}
203201
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+
204230# -------------------------------------------------------------------------------- #
205231# Parse ini file #
206232# -------------------------------------------------------------------------------- #
@@ -220,19 +246,19 @@ function process_ini_file()
220246 while read -r line; do
221247 line_number=$(( line_number+ 1 ))
222248
223- if [[ $line =~ ^# || -z $line ]]; then # Ignore comments / empty lines
249+ if [[ $line =~ ^# || -z $line ]]; then # Ignore comments / empty lines
224250 continue ;
225251 fi
226252
227- if [[ $line =~ ^" [" (.+)" ]" $ ]]; then # Match pattern for a 'section'
253+ if [[ $line =~ ^" [" (.+)" ]" $ ]]; then # Match pattern for a 'section'
228254 section=$( process_section_name " ${BASH_REMATCH[1]} " )
229255
230256 if ! in_array sections " ${section} " ; then
231257 eval " ${section} _keys=()" # Use eval to declare the keys array
232258 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
234260 fi
235- elif [[ $line =~ ^(.* )" =" (.* ) ]]; then # Match patter for a key=value pair
261+ elif [[ $line =~ ^(.* )" =" (.* ) ]]; then # Match patter for a key=value pair
236262 key=$( process_key_name " ${BASH_REMATCH[1]} " )
237263 value=$( process_value " ${BASH_REMATCH[2]} " )
238264
@@ -280,7 +306,8 @@ function get_value()
280306
281307 for i in " ${! keys[@]} " ; do
282308 if [[ " ${keys[$i]} " = " ${key} " ]]; then
283- printf ' %s' " ${values[$i]} "
309+ orig=$( unescape_string " ${values[$i]} " )
310+ printf ' %s' " ${orig} "
284311 fi
285312 done
286313}
@@ -308,7 +335,8 @@ function display_config()
308335 eval " values=( \"\$ {${section} _values[@]}\" )"
309336
310337 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} "
312340 done
313341 printf ' \n'
314342 done
@@ -336,7 +364,8 @@ function display_config_by_section()
336364 eval " values=( \"\$ {${section} _values[@]}\" )"
337365
338366 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} "
340369 done
341370 printf ' \n'
342371}
0 commit comments