From 538593f9c3b46eb11ede31ed146fbd700ba6ba65 Mon Sep 17 00:00:00 2001 From: Maciej Pasternacki Date: Tue, 11 Nov 2008 16:42:03 +0100 Subject: [PATCH] - Add Bash .ini parser --- src/ini.bash | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 src/ini.bash diff --git a/src/ini.bash b/src/ini.bash new file mode 100644 index 0000000..2ba95e1 --- /dev/null +++ b/src/ini.bash @@ -0,0 +1,21 @@ +# http://ajdiaz.wordpress.com/2008/02/09/bash-ini-parser/ + +function cfg.parser () +{ + IFS=$'\n' && ini=( $(<$1) ) # convert to line-array + ini=( ${ini[*]//;*/} ) # remove comments `;' + ini=( ${ini[*]//\#*/} ) # remove comments `#' + ini=( ${ini[*]/\ =\ /=} ) # remove anything with a space around `=` + ini=( ${ini[*]/#[/\}$'\n'cfg.section.} ) # set section prefix + ini=( ${ini[*]/%]/ \(} ) # convert text2function (1) + ini=( ${ini[*]/=/=\( } ) # convert item to array + ini=( ${ini[*]/%/ \)} ) # close array parenthesis + ini=( ${ini[*]/%\( \)/\(\) \{} ) # convert text2function (2) + ini=( ${ini[*]/%\} \)/\}} ) # remove extra parenthesis + ini=( ${ini[*]/#\ */} ) # remove blank lines + ini=( ${ini[*]/#\ */} ) # remove blank lines with tabs + ini[0]='' # remove first element + ini[${#ini[*]} + 1]='}' # add the last brace + # printf "%s\n" ${ini[*]} + eval "$(echo "${ini[*]}")" # eval the result +} -- 2.11.4.GIT