LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-04-2009, 03:57 AM   #1
zhjim
Senior Member
 
Registered: Oct 2004
Distribution: Debian Squeeze x86_64
Posts: 1,748
Blog Entries: 11

Rep: Reputation: 233Reputation: 233Reputation: 233
Another way of declaring variables in bash


Hi folks,

I have a little bash script that greps some info out of a config file. The way I'm doing this is using a for loop

Code:
for i in name disk vif; do
        DUMMY=$(grep "^[::space::]*$i" $CONF)
        DUMMY=$(echo $DUMMY | cut -d = -f 2-)


        # Get middle of "
        DUMMY=$(echo $DUMMY | sed 's/^.*"\(.*\)".*$/\1/g')      #DUMMY=${DUMMY#*\"}; #DUMMY=${DUMMY%*\"}
        # Get middle of '
        DUMMY=$(echo $DUMMY | sed "s/^.*'\(.*\)'.*$/\1/g")      #DUMMY=${DUMMY#*\'}; #DUMMY=${DUMMY%*\'}
      
        {$i}=\${$DUMMY}
done
The part were I'm lost is the last line inside the loop "{$i}=\${$DUMMY}.
What I'm trying to do is to have the value of $DUMMY to be assigned to a variable with the name of contents of $i.
So in the end I should have three variables with the names of $names, $disk, $vif

Is this possible at all?

Thanks in advance
Zhjim
 
Old 05-04-2009, 04:25 AM   #2
ChrisAbela
Member
 
Registered: Mar 2008
Location: Malta
Distribution: Slackware
Posts: 572

Rep: Reputation: 154Reputation: 154
"$" is a meta character, you should no use as part of the varaible name, or else it gets confusing.

If it is "i" that you would like assign a value then I suggest:

i=$DUMMY

Note that you already assigned a value to i in the "for" line. So "i" is already assigned the vlaues of "disk" and "vif".
 
Old 05-04-2009, 04:46 AM   #3
linterrogate
LQ Newbie
 
Registered: Mar 2008
Distribution: debian
Posts: 8

Rep: Reputation: 0
Quote:
Originally Posted by zhjim View Post
Hi folks,

I have a little bash script that greps some info out of a config file. The way I'm doing this is using a for loop

Code:
for i in name disk vif; do
        DUMMY=$(grep "^[::space::]*$i" $CONF)
        DUMMY=$(echo $DUMMY | cut -d = -f 2-)


        # Get middle of "
        DUMMY=$(echo $DUMMY | sed 's/^.*"\(.*\)".*$/\1/g')      #DUMMY=${DUMMY#*\"}; #DUMMY=${DUMMY%*\"}
        # Get middle of '
        DUMMY=$(echo $DUMMY | sed "s/^.*'\(.*\)'.*$/\1/g")      #DUMMY=${DUMMY#*\'}; #DUMMY=${DUMMY%*\'}
      
        {$i}=\${$DUMMY}
done
The part were I'm lost is the last line inside the loop "{$i}=\${$DUMMY}.
What I'm trying to do is to have the value of $DUMMY to be assigned to a variable with the name of contents of $i.
So in the end I should have three variables with the names of $names, $disk, $vif

Is this possible at all?

Thanks in advance
Zhjim
how about writing a python script instead, using true associative arrays. Your script will work, but the array indexes must evaluate to numeric values
 
Old 05-04-2009, 09:42 AM   #4
Kenhelm
Member
 
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 360

Rep: Reputation: 170Reputation: 170
Try using 'eval'
Code:
name=
i=name
DUMMY=abc

eval $i=\$DUMMY

echo $name
abc
 
Old 05-04-2009, 12:18 PM   #5
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,928

Rep: Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612
bash indirect substituttion:
${!i}=\${$DUMMY}
 
Old 05-04-2009, 04:07 PM   #6
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
why not just have the config file as a shell script and source it?

Code:
name=blah
age=humbug
Code:
. config
 
Old 05-05-2009, 02:13 AM   #7
ChrisAbela
Member
 
Registered: Mar 2008
Location: Malta
Distribution: Slackware
Posts: 572

Rep: Reputation: 154Reputation: 154
You can also place them in .bash_profile (or .bashrc, depending on your distro)

# export NAME=blah
# export AGE=humbag

The possibilites are endless
Let it never be said that Linux Users do net get support :-).
 
Old 05-11-2009, 08:57 AM   #8
zhjim
Senior Member
 
Registered: Oct 2004
Distribution: Debian Squeeze x86_64
Posts: 1,748

Original Poster
Blog Entries: 11

Rep: Reputation: 233Reputation: 233Reputation: 233
Thanks for all the input. Got it working with eval, but changed it a bit to work

Code:
for i in name disk vif; do
        DUMMY=$(grep "^[::space::]*$i" $CONF)
        DUMMY=$(echo $DUMMY | cut -d = -f 2-)


        # Get middle of "
        DUMMY=$(echo $DUMMY | sed 's/^.*"\(.*\)".*$/\1/g')      #DUMMY=${DUMMY#*\"}; #DUMMY=${DUMMY%*\"}
        # Get middle of '
        DUMMY=$(echo $DUMMY | sed "s/^.*'\(.*\)'.*$/\1/g")      #DUMMY=${DUMMY#*\'}; #DUMMY=${DUMMY%*\'}

        eval $i=$DUMMY

done

echo $name
echo $disk
echo $vif
Code:
# ./mkximg /etc/xen/apache
apache
phy:/dev/sdb2,sda1,w
ip=10.0.0.2
Now onward to get the real information out of the last to vars.

Quote:
Originally Posted by ChrisAbela
The possibilites are endless
Let it never be said that Linux Users do net get support :-)
That's why I'm loving this *little* OS

Cheers Zhjim
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Python Reading and Declaring Variables from a Text File dudeman41465 Programming 7 01-31-2009 04:54 PM
Bash Variables SlipDigby Programming 2 09-01-2008 05:00 PM
Help With Variables In Bash jamie_h Programming 3 11-01-2006 08:18 AM
difference between () and = when declaring variables nadroj Programming 1 06-28-2005 01:37 PM
Bash variables pk21 Programming 2 01-09-2003 03:31 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:50 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration