Help with dynamic configure cfg files

hi; i have one configuration file(configuration.cfg),where contents are below..

filename = charge.cfg
sectionname = [AUTORETRYTIMES]
networkid[1] = 1
retrytimes[1] = 2
-------------
--------------
sectionname = [CHARGE]
networkid[2] = 1
retrytimes[2] = 2
filename = xyz.cfg
------------------
-----------------

There is multiple filename & section name's.
How i read filename and section name and also between lines in two diff. sections dynamically ?
Then search as per filename in config directory,if found,then search for section name,if found then append/insert the lines in that file in shell script(linux).

welcome to the forum.

Your order is not fixed. Is that true?

for the first set, you have filename then section name.
in that later, first you have section name and filename in last?!!

Should this act as you want?

#!/bin/bash
read -p "Section name: " SECTION
read -p "File name: " FILE
until [ "$L" = "filename = $FILE" ]; do read L; done # seek file name
until [ "$L" = "sectionname = [$SECTION]" ]; do read L; done # seek section name
IFS='='
while read A B
do
    [ "$VAR" = filename ] || [ "$VAR" = sectionname ] && break # if next file or section encountered
    eval "$A=${B:1}" # ${B:1} to cut the first char (space)
done < configuration.cfg

---------- Post updated at 19:55 ---------- Previous update was at 19:42 ----------

If there were no spaces around the '=', you could simply do somethin like

read
eval "$REPLY"

Actually anchal in configure.cfg file,so many filenames are there and under one filename so many sectionname's,and under each section so many parameter's.

from the input file, what's the output you expect?

if the session grouped by "----------------------"

Hi rdcwayx;
Actually i am expecting filename first from input file and search in bin folder for that filename,if found,then expecting sectionname from input file and again search that section available in that filename,if found,then iam expecting all parameter's from input file(parameter's are reside in input file is like this,may be in between two sections || may be in between sectionname and filename || section name and eof),then i am appending/inserting those parameter's in that section(destination file's section) .
Again in destination file,many probality,means taht file consist only many || one sectionname(like [charge]) and parameter's.when we found sectionname from destination file,we have to check the position where we insert/append parameter's taking from input file.again we encounter multiple section and finally eof in destination file.

---------- Post updated at 12:12 AM ---------- Previous update was at 12:02 AM ----------

Hi frans;
I am not expecting input from console,input is file(configure.cfg) and changes will happen in bin/filename,comes from configure.cfg.

Maybe, this should do the job (take care : it modifies the config files (make a backup))

#!/bin/bash
while read A S B
do
    case $A in
        filename)    # process new file
            FILE=bin/$B
            unset SECT
            [ -f $FILE ] || { echo "$FILE not found"; unset FILE; }
        ;;
        sectionname) # new section
            [ -z "$FILE" ] && continue
            SECT="$B"
            # return the line number of section in $FILE
            N=$(grep -Fwn "$SECT" $FILE | cut -d':' -f1) || { echo "$SECT not found in $FILE "; unset SECT; }
        ;;
        *)    # process parameter
            if [ -n "$FILE" ] && [ -n "$SECT" ]
            then
                ((N++))
                sed -i "${N}i\n$A $S $B" $FILE
            fi
        ;;
    esac
done < configuration.cfg

thanks for your codesnap,but it is not working,it's never enter case.

---------- Post updated at 06:20 AM ---------- Previous update was at 04:11 AM ----------

Hi frans;
Really nice code,now it's working fine after little modification,but it's writting in destnation file with leading with a space how i remove it.for example...
spaceparameter1
spaceparameter2
like this,it's mismatch the existing parameter.

---------- Post updated at 06:26 AM ---------- Previous update was at 06:20 AM ----------

Hi frans;
Really nice code,now it's working fine after little modification,but it's writting in destnation file with leading with a space how i remove it.for example...
spaceparameter1
spaceparameter2
like this,

---------- Post updated at 08:52 AM ---------- Previous update was at 06:26 AM ----------

Hi frans;
your code is nice working after little bit changes,but when inserting parametres in destination files,start with space...like
spaceParameter1
spaceParameter2
..................
how i remove this leading space?
Now this issue is resolved.
Thanks