You've already forked bash-ini-parser
mirror of
https://github.com/albfan/bash-ini-parser.git
synced 2025-06-12 19:41:51 +03:00
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
|
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
|
### 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)
|
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 {
|
function cfg_writer {
|
||||||
|
OLDIFS="$IFS"
|
||||||
IFS=' '$'\n'
|
IFS=' '$'\n'
|
||||||
fun="$(declare -F)"
|
fun="$(declare -F)"
|
||||||
fun="${fun//declare -f/}"
|
fun="${fun//declare -f/}"
|
||||||
for f in $fun; do
|
for f in $fun; do
|
||||||
[ "${f#$PREFIX}" == "${f}" ] && continue
|
[ "${f#$PREFIX}" == "${f}" ] && continue
|
||||||
item="$(declare -f ${f})"
|
item="$(declare -f ${f})"
|
||||||
item="${item##*\{}"
|
item="${item##*\{}" # remove function definition
|
||||||
item="${item/\}}"
|
item="${item/\}}" # remove function close
|
||||||
item="${item%)*}"
|
item="${item%)*}" # remove everything after parenthesis
|
||||||
item="${item});"
|
item="${item});" # add close parenthesis
|
||||||
vars=""
|
vars=""
|
||||||
while [ "$item" != "" ]
|
while [ "$item" != "" ]
|
||||||
do
|
do
|
||||||
newvar="${item%%=*}"
|
newvar="${item%%=*}" # get item name
|
||||||
vars="$vars $newvar"
|
vars="$vars $newvar" # add name to collection
|
||||||
item="${item#*;}"
|
item="${item#*;}" # remove readed line
|
||||||
done
|
done
|
||||||
eval $f
|
eval $f
|
||||||
echo "[${f#$PREFIX}]"
|
echo "[${f#$PREFIX}]" # output section
|
||||||
for var in $vars; do
|
for var in $vars; do
|
||||||
eval 'local length=${#'$var'[*]}'
|
eval 'local length=${#'$var'[*]}' # test if var is an array
|
||||||
if [ $length == 1 ]
|
if [ $length == 1 ]
|
||||||
then
|
then
|
||||||
echo $var=\"${!var}\"
|
echo $var=\"${!var}\" #output var
|
||||||
else
|
else
|
||||||
echo ";$var is an array"
|
echo ";$var is an array" # add comment denoting var is an array
|
||||||
eval 'echo $var=\"${'$var'[*]}\"'
|
eval 'echo $var=\"${'$var'[*]}\"' # output array var
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
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
|
# vim: filetype=sh
|
||||||
|
@ -32,3 +32,21 @@ echo enable section \'sec1\'
|
|||||||
cfg_section_sec1
|
cfg_section_sec1
|
||||||
|
|
||||||
echo "var2 value is \"$var2\""
|
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\"
|
||||||
|
Reference in New Issue
Block a user