mirror of
https://github.com/albfan/bash-ini-parser.git
synced 2025-04-25 09:42:31 +03:00
parent
3a8520b348
commit
20466a593c
13
README.md
13
README.md
@ -39,6 +39,19 @@ Goto scripts directory and launch [example.sh](https://github.com/albfan/bash-in
|
||||
|
||||
Inspect its code, reuse on your scripts
|
||||
|
||||
### Updating and saving changes
|
||||
|
||||
To update a value
|
||||
|
||||
var=new_value
|
||||
cfg_update <sec> <var>
|
||||
|
||||
To save changes
|
||||
|
||||
cfg_writer > newfile.ini
|
||||
|
||||
> Take care that saving function will loose comments and indentation, use with care
|
||||
|
||||
### Checking a ini file
|
||||
|
||||
If you want to test your existing ini file use [getkeyfromsection.sh](https://github.com/albfan/bash-ini-parser/blob/master/scripts/getkeyfromsection.sh)
|
||||
|
@ -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
|
||||
|
@ -32,3 +32,21 @@ echo enable section \'sec1\'
|
||||
cfg_section_sec1
|
||||
|
||||
echo "var2 value is \"$var2\""
|
||||
|
||||
echo
|
||||
echo update values:
|
||||
echo
|
||||
|
||||
echo update sec1.var1 to \"foobar\"
|
||||
var1="foobar"
|
||||
cfg_update sec1 var1
|
||||
echo update sec2.var1 to \"barfoo\"
|
||||
var1="barfoo"
|
||||
cfg_update sec2 var1
|
||||
|
||||
# note you don't need to load section to update it
|
||||
cfg_section_sec1
|
||||
echo sec1.var1 value is \"$var1\"
|
||||
|
||||
cfg_section_sec2
|
||||
echo sec2.var1 value is \"$var1\"
|
||||
|
Loading…
x
Reference in New Issue
Block a user