From 3a8520b348f69a4739fff816889ec31ff38b9446 Mon Sep 17 00:00:00 2001 From: albfan Date: Fri, 15 May 2015 18:55:44 +0200 Subject: [PATCH] choose a more standard prefix closes #1 --- README.md | 4 ++-- bash-ini-parser | 8 +++++--- scripts/example.sh | 4 ++-- scripts/getkeyfromsection.sh | 2 +- 4 files changed, 10 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 9841032..f04eec8 100644 --- a/README.md +++ b/README.md @@ -22,9 +22,9 @@ Issuing: $ cfg_parser file.ini -Will declare functions per ini section called cfg.section.
which declares variables named as keynames so you can access its values using +Will declare functions per ini section called cfg_section_<section> which declares variables named as keynames so you can access its values using - $ cfg.section.
+ $ cfg_section_
$ echo $key value $ echo $key2 diff --git a/bash-ini-parser b/bash-ini-parser index 1f9339d..cf55d5b 100644 --- a/bash-ini-parser +++ b/bash-ini-parser @@ -2,6 +2,8 @@ # based on http://theoldschooldevops.com/2008/02/09/bash-ini-parser/ # +PREFIX="cfg_section_" + function debug { return #abort debug echo $* @@ -29,7 +31,7 @@ function cfg_parser { debug "whitespace around" ini=( ${ini[*]/*([[:space:]])=*([[:space:]])/=} ) # remove whitespace around = debug - ini=( ${ini[*]/#\\[/\}$'\n'cfg.section.} ) # set section prefix + ini=( ${ini[*]/#\\[/\}$'\n'"$PREFIX"} ) # set section prefix debug ini=( ${ini[*]/%\\]/ \(} ) # convert text2function (1) debug @@ -60,7 +62,7 @@ function cfg_writer { fun="$(declare -F)" fun="${fun//declare -f/}" for f in $fun; do - [ "${f#cfg.section}" == "${f}" ] && continue + [ "${f#$PREFIX}" == "${f}" ] && continue item="$(declare -f ${f})" item="${item##*\{}" item="${item/\}}" @@ -74,7 +76,7 @@ function cfg_writer { item="${item#*;}" done eval $f - echo "[${f#cfg.section.}]" + echo "[${f#$PREFIX}]" for var in $vars; do eval 'local length=${#'$var'[*]}' if [ $length == 1 ] diff --git a/scripts/example.sh b/scripts/example.sh index 1bfa6bd..ccb0bd0 100755 --- a/scripts/example.sh +++ b/scripts/example.sh @@ -20,7 +20,7 @@ echo show some results: echo echo enable section \'sec2\' -cfg.section.sec2 +cfg_section_sec2 echo "var2 value is \"$var2\"" echo "var5[1] value is \"${var5[1]}"\" @@ -29,6 +29,6 @@ echo "var4 value is \"$var4"\" echo echo enable section \'sec1\' -cfg.section.sec1 +cfg_section_sec1 echo "var2 value is \"$var2\"" diff --git a/scripts/getkeyfromsection.sh b/scripts/getkeyfromsection.sh index 0596780..95765d1 100755 --- a/scripts/getkeyfromsection.sh +++ b/scripts/getkeyfromsection.sh @@ -15,7 +15,7 @@ cfg_writer echo # enable section selected -cfg.section.$TEST_SECTION +cfg_section_$TEST_SECTION # show value of section selected echo "$TEST_VALUE value is \"${!TEST_VALUE}\""