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 {
if ! [ "x$BASH_INI_PARSER_DEBUG" == "x" ]
then
echo $*
echo --start--
echo
echo --start-- $*
echo "${ini[*]}"
echo --end--
echo
@@ -39,10 +39,21 @@ function cfg_parser {
debug
ini=( ${ini[*]/%+([[:space:]])/} ) # remove ending whitespace
debug "whitespace around ="
ini=( ${ini[*]/%+([[:space:]])\\]/\\]} ) # remove non meaningful whitespace after sections
debug "whitespace around ="
ini=( ${ini[*]/*([[:space:]])=*([[:space:]])/=} ) # remove whitespace around =
debug
ini=( ${ini[*]/#\\[/\}$'\n'"$PREFIX"} ) # set section prefix
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)
debug
ini=( ${ini[*]/=/=\( } ) # convert item to array

View File

@@ -17,4 +17,7 @@ var5=section 2 VAR 5
var1="section 3 VAR 1"
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]
var1="section 3 VAR 1"
var2="section 3 VAR 2"
[section4_subsec]
var1="section 3 VAR 1"
var2="section 3 VAR 2"