LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 11-18-2011, 03:47 AM   #1
nano2
Member
 
Registered: May 2007
Posts: 100

Rep: Reputation: 15
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 ?

Last edited by nano2; 11-18-2011 at 09:14 AM.
 
Old 11-18-2011, 05:25 AM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,541
Blog Entries: 28

Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
As the script sources the config file it could be
Code:
name=( typeA typeB typeC typeD typeE )
and $name would be an array ...
 
Old 11-18-2011, 08:31 AM   #3
nano2
Member
 
Registered: May 2007
Posts: 100

Original Poster
Rep: Reputation: 15
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 ...... )
 
Old 11-18-2011, 08:42 AM   #4
nano2
Member
 
Registered: May 2007
Posts: 100

Original Poster
Rep: Reputation: 15
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 ?
 
  


Reply

Tags
bash scripting


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash + Reading values(numbers) from a file and storing them into an array SuchANewb Linux - Newbie 5 11-04-2010 08:11 AM
[bash] indirect array reference to array with values containing spaces Meson Linux - Software 9 06-04-2010 09:38 PM
php array... can I have more than two values per entry? eco Programming 3 06-15-2009 04:51 PM
extracting values from an array bahadur Programming 2 08-14-2006 01:07 AM
help with shell script - fill an array with values from a file delmoras Linux - General 1 07-17-2006 11:19 AM


All times are GMT -5. The time now is 05:20 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration