1
0
mirror of https://github.com/albfan/bash-ini-parser.git synced 2025-08-09 05:22:44 +03:00

Allow subsections

This commit is contained in:
Alberto Fanjul
2020-06-22 14:43:57 +02:00
parent df7ed8550d
commit d9dfb77033
3 changed files with 19 additions and 2 deletions

View File

@@ -7,8 +7,8 @@ PREFIX="cfg_section_"
function debug { function debug {
if ! [ "x$BASH_INI_PARSER_DEBUG" == "x" ] if ! [ "x$BASH_INI_PARSER_DEBUG" == "x" ]
then then
echo $* echo
echo --start-- echo --start-- $*
echo "${ini[*]}" echo "${ini[*]}"
echo --end-- echo --end--
echo echo
@@ -39,10 +39,21 @@ function cfg_parser {
debug debug
ini=( ${ini[*]/%+([[:space:]])/} ) # remove ending whitespace ini=( ${ini[*]/%+([[:space:]])/} ) # remove ending whitespace
debug "whitespace around =" debug "whitespace around ="
ini=( ${ini[*]/%+([[:space:]])\\]/\\]} ) # remove non meaningful whitespace after sections
debug "whitespace around ="
ini=( ${ini[*]/*([[:space:]])=*([[:space:]])/=} ) # remove whitespace around = ini=( ${ini[*]/*([[:space:]])=*([[:space:]])/=} ) # remove whitespace around =
debug debug
ini=( ${ini[*]/#\\[/\}$'\n'"$PREFIX"} ) # set section prefix ini=( ${ini[*]/#\\[/\}$'\n'"$PREFIX"} ) # set section prefix
debug debug
for ((i=0; i < "${#ini[@]}"; i++))
do
line="${ini[i]}"
if [[ "$line" =~ $PREFIX.+ ]]
then
ini[$i]=${line// /_}
fi
done
debug "subsections"
ini=( ${ini[*]/%\\]/ \(} ) # convert text2function (1) ini=( ${ini[*]/%\\]/ \(} ) # convert text2function (1)
debug debug
ini=( ${ini[*]/=/=\( } ) # convert item to array ini=( ${ini[*]/=/=\( } ) # convert item to array

View File

@@ -17,4 +17,7 @@ var5=section 2 VAR 5
var1="section 3 VAR 1" var1="section 3 VAR 1"
var2= "section 3 VAR 2" var2= "section 3 VAR 2"
[section4 subsec]
var1="section 3 VAR 1"
var2= "section 3 VAR 2"

View File

@@ -15,3 +15,6 @@ var5="section 2 VAR 5"
[section3] [section3]
var1="section 3 VAR 1" var1="section 3 VAR 1"
var2="section 3 VAR 2" var2="section 3 VAR 2"
[section4_subsec]
var1="section 3 VAR 1"
var2="section 3 VAR 2"