LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (http://www.linuxquestions.org/questions/linux-general-1/)
-   -   create an array map of values frm a configuration file (http://www.linuxquestions.org/questions/linux-general-1/create-an-array-map-of-values-frm-a-configuration-file-914206/)

nano2 11-18-2011 04:47 AM

create an array map of values frm a configuration file
 
Hi ,

I have the following scenario where by I have configuration file that has the following :

Code:


cfgFile
############################
#################################
name=typeA,typeB,typeC,typeD,typeE

###########################
Server=testS1,testS2,testS3,testS4,testS5
##########################################
Client=testCA,testCB,testCC,testCD,testCE

###########################################

I want t create a mapping of the above
typeA->testS1->testC1
typeB->testS2->testC2
typeC->testS3->testC3
and so on

so in my bash script i read the cfgFile that will use the name to determine
the Server and Client


So when a user enters choice typeA
Server =testS1 and Client=testCA



Code:


MYDIR=`pwd`

if [ -f  $MYDIR/cfgFile ];then

        . $MYDIR/cfgFile

else

        error "error  $cfgFile doesn't exist"

        exit 1
fi



echo "enter Choice > "
read choice


if [ $choice == "typeA" ] ; then
    echo "Client = $client "
    echo "Server= $server"
fi

any ideas how i could do this ?

catkin 11-18-2011 06:25 AM

As the script sources the config file it could be
Code:

name=( typeA typeB typeC typeD typeE )
and $name would be an array ...

nano2 11-18-2011 09:31 AM

I am trying to read the values of NAME from the config file into array like so

Code:


while read -r line; do
                #echo  "line == $line"
                if [ $line == name ]; then
                        l2=${line##*=}
                     
                        IFS=" ," arr=( $l2 )
                        echo  " the name array ${arr[@]}"
                fi
done < $CFGFILE


It won't work any one spot why I need the name array( typeA typeB ...... )

nano2 11-18-2011 09:42 AM

I have also tried to use
Code:


array=(`sed -n "s/^[^name].*=\(.*\)/\1/p" $CFGFILE)
echo  " the array ${array[@]}"

But this won't work anyone knows why ?


All times are GMT -5. The time now is 08:13 AM.