diff --git a/README.md b/README.md index c77a18c..9bc5fe6 100644 --- a/README.md +++ b/README.md @@ -89,7 +89,6 @@ Outputs: This is more a hack than a reliable parser, so keep in mind things like - multiword value vars will be arrays so you must access it as `${var[*]}` - - changing from section to section will not erase previous var declarations For a trusted parser (but based on python) checkout [crudini](https://github.com/pixelb/crudini) diff --git a/bash-ini-parser b/bash-ini-parser index adc9bc0..5fb7027 100644 --- a/bash-ini-parser +++ b/bash-ini-parser @@ -46,6 +46,7 @@ function cfg_parser { ini=( ${ini[*]/%\( \)/\(\) \{} ) # convert text2function (2) debug ini=( ${ini[*]/%\} \)/\}} ) # remove extra parenthesis + ini=( ${ini[*]/%\{/\{$'\n''cfg_unset ${FUNCNAME/#'$PREFIX'}'$'\n'} ) # clean previous definition of section debug ini[0]="" # remove first element debug @@ -68,6 +69,7 @@ function cfg_writer { [ "${f#$PREFIX}" == "${f}" ] && continue item="$(declare -f ${f})" item="${item##*\{}" # remove function definition + item="${item##*FUNCNAME*$PREFIX\};}" # remove clear section item="${item/\}}" # remove function close item="${item%)*}" # remove everything after parenthesis item="${item});" # add close parenthesis @@ -94,6 +96,44 @@ function cfg_writer { IFS="$OLDIFS" } +function cfg_unset { + SECTION=$1 + OLDIFS="$IFS" + IFS=' '$'\n' + if [ -z "$SECTION" ] + then + fun="$(declare -F)" + else + fun="$(declare -F $PREFIX$SECTION)" + if [ -z "$fun" ] + then + echo "section $SECTION not found" >2 + return + fi + fi + fun="${fun//declare -f/}" + for f in $fun; do + [ "${f#$PREFIX}" == "${f}" ] && continue + item="$(declare -f ${f})" + item="${item##*\{}" # remove function definition + item="${item##*FUNCNAME*$PREFIX\};}" # remove clear section + item="${item/\}}" # remove function close + item="${item%)*}" # remove everything after parenthesis + item="${item});" # add close parenthesis + vars="" + while [ "$item" != "" ] + do + newvar="${item%%=*}" # get item name + vars="$vars $newvar" # add name to collection + item="${item#*;}" # remove readed line + done + for var in $vars; do + unset $var + done + done + IFS="$OLDIFS" +} + function cfg_clear { OLDIFS="$IFS" IFS=' '$'\n'