1
0
mirror of https://github.com/albfan/bash-ini-parser.git synced 2025-08-04 19:42:06 +03:00

allow updates

closes #3
This commit is contained in:
albfan
2015-05-15 20:01:17 +02:00
parent 3a8520b348
commit 20466a593c
3 changed files with 70 additions and 12 deletions

View File

@@ -58,36 +58,63 @@ function cfg_parser {
}
function cfg_writer {
OLDIFS="$IFS"
IFS=' '$'\n'
fun="$(declare -F)"
fun="${fun//declare -f/}"
for f in $fun; do
[ "${f#$PREFIX}" == "${f}" ] && continue
item="$(declare -f ${f})"
item="${item##*\{}"
item="${item/\}}"
item="${item%)*}"
item="${item});"
item="${item##*\{}" # remove function definition
item="${item/\}}" # remove function close
item="${item%)*}" # remove everything after parenthesis
item="${item});" # add close parenthesis
vars=""
while [ "$item" != "" ]
do
newvar="${item%%=*}"
vars="$vars $newvar"
item="${item#*;}"
newvar="${item%%=*}" # get item name
vars="$vars $newvar" # add name to collection
item="${item#*;}" # remove readed line
done
eval $f
echo "[${f#$PREFIX}]"
echo "[${f#$PREFIX}]" # output section
for var in $vars; do
eval 'local length=${#'$var'[*]}'
eval 'local length=${#'$var'[*]}' # test if var is an array
if [ $length == 1 ]
then
echo $var=\"${!var}\"
echo $var=\"${!var}\" #output var
else
echo ";$var is an array"
eval 'echo $var=\"${'$var'[*]}\"'
echo ";$var is an array" # add comment denoting var is an array
eval 'echo $var=\"${'$var'[*]}\"' # output array var
fi
done
done
IFS="$OLDIFS"
}
function cfg_update {
SECTION=$1
VAR=$2
OLDIFS="$IFS"
IFS=' '$'\n'
fun="$(declare -F $PREFIX$SECTION)"
if [ -z $fun ]
then
echo "section $SECTION not found" >2
exit 1
fi
fun="${fun//declare -f/}"
item="$(declare -f ${fun})"
#item="${item##* $VAR=*}" # remove var declaration
item="${item/\}}" # remove function close
item="${item}
$VAR=(${!VAR})
"
item="${item}
}" # close function again
eval "function $item"
}
# vim: filetype=sh