You've already forked bash-ini-parser
mirror of
https://github.com/albfan/bash-ini-parser.git
synced 2025-08-06 07:02:37 +03:00
unset function
clear previous definition (if exist) before load it closes #6
This commit is contained in:
@@ -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)
|
||||
|
||||
|
@@ -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'
|
||||
|
Reference in New Issue
Block a user