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
@@ -22,9 +22,9 @@ Issuing:
|
|||||||
|
|
||||||
$ cfg_parser file.ini
|
$ cfg_parser file.ini
|
||||||
|
|
||||||
Will declare functions per ini section called cfg.section.<section> which declares variables named as keynames so you can access its values using
|
Will declare functions per ini section called cfg_section_<section> which declares variables named as keynames so you can access its values using
|
||||||
|
|
||||||
$ cfg.section.<section>
|
$ cfg_section_<section>
|
||||||
$ echo $key
|
$ echo $key
|
||||||
value
|
value
|
||||||
$ echo $key2
|
$ echo $key2
|
||||||
|
@@ -2,6 +2,8 @@
|
|||||||
# based on http://theoldschooldevops.com/2008/02/09/bash-ini-parser/
|
# based on http://theoldschooldevops.com/2008/02/09/bash-ini-parser/
|
||||||
#
|
#
|
||||||
|
|
||||||
|
PREFIX="cfg_section_"
|
||||||
|
|
||||||
function debug {
|
function debug {
|
||||||
return #abort debug
|
return #abort debug
|
||||||
echo $*
|
echo $*
|
||||||
@@ -29,7 +31,7 @@ function cfg_parser {
|
|||||||
debug "whitespace around"
|
debug "whitespace around"
|
||||||
ini=( ${ini[*]/*([[:space:]])=*([[:space:]])/=} ) # remove whitespace around =
|
ini=( ${ini[*]/*([[:space:]])=*([[:space:]])/=} ) # remove whitespace around =
|
||||||
debug
|
debug
|
||||||
ini=( ${ini[*]/#\\[/\}$'\n'cfg.section.} ) # set section prefix
|
ini=( ${ini[*]/#\\[/\}$'\n'"$PREFIX"} ) # set section prefix
|
||||||
debug
|
debug
|
||||||
ini=( ${ini[*]/%\\]/ \(} ) # convert text2function (1)
|
ini=( ${ini[*]/%\\]/ \(} ) # convert text2function (1)
|
||||||
debug
|
debug
|
||||||
@@ -60,7 +62,7 @@ function cfg_writer {
|
|||||||
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#cfg.section}" == "${f}" ] && continue
|
[ "${f#$PREFIX}" == "${f}" ] && continue
|
||||||
item="$(declare -f ${f})"
|
item="$(declare -f ${f})"
|
||||||
item="${item##*\{}"
|
item="${item##*\{}"
|
||||||
item="${item/\}}"
|
item="${item/\}}"
|
||||||
@@ -74,7 +76,7 @@ function cfg_writer {
|
|||||||
item="${item#*;}"
|
item="${item#*;}"
|
||||||
done
|
done
|
||||||
eval $f
|
eval $f
|
||||||
echo "[${f#cfg.section.}]"
|
echo "[${f#$PREFIX}]"
|
||||||
for var in $vars; do
|
for var in $vars; do
|
||||||
eval 'local length=${#'$var'[*]}'
|
eval 'local length=${#'$var'[*]}'
|
||||||
if [ $length == 1 ]
|
if [ $length == 1 ]
|
||||||
|
@@ -20,7 +20,7 @@ echo show some results:
|
|||||||
|
|
||||||
echo
|
echo
|
||||||
echo enable section \'sec2\'
|
echo enable section \'sec2\'
|
||||||
cfg.section.sec2
|
cfg_section_sec2
|
||||||
|
|
||||||
echo "var2 value is \"$var2\""
|
echo "var2 value is \"$var2\""
|
||||||
echo "var5[1] value is \"${var5[1]}"\"
|
echo "var5[1] value is \"${var5[1]}"\"
|
||||||
@@ -29,6 +29,6 @@ echo "var4 value is \"$var4"\"
|
|||||||
|
|
||||||
echo
|
echo
|
||||||
echo enable section \'sec1\'
|
echo enable section \'sec1\'
|
||||||
cfg.section.sec1
|
cfg_section_sec1
|
||||||
|
|
||||||
echo "var2 value is \"$var2\""
|
echo "var2 value is \"$var2\""
|
||||||
|
@@ -15,7 +15,7 @@ cfg_writer
|
|||||||
echo
|
echo
|
||||||
|
|
||||||
# enable section selected
|
# enable section selected
|
||||||
cfg.section.$TEST_SECTION
|
cfg_section_$TEST_SECTION
|
||||||
|
|
||||||
# show value of section selected
|
# show value of section selected
|
||||||
echo "$TEST_VALUE value is \"${!TEST_VALUE}\""
|
echo "$TEST_VALUE value is \"${!TEST_VALUE}\""
|
||||||
|
Reference in New Issue
Block a user