LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
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 04-27-2004, 11:15 AM   #1
sdandeker
LQ Newbie
 
Registered: Sep 2003
Location: UK
Distribution: Gentoo
Posts: 22

Rep: Reputation: 15
calling a variable within a variable


HELP!

I want to call a variable within a variable. I want to be able to use a loop that substitutes a variable name in the program.

For example:

monitor="mypcp"
mypcp="192.168.254.254"

Now I want the result 192.168.254.254 using the variable monitor!

Doing a echo $monitor gives mypcp
Doing a echo "$"$monitor"" gives $mypcp

Any ideas?
 
Old 04-27-2004, 01:18 PM   #2
AutOPSY
Member
 
Registered: Mar 2004
Location: US
Distribution: Redhat 9 - Linux 2.6.3
Posts: 836

Rep: Reputation: 31
monitor=$mypcp
mypcp="192.186.0.1"

echo $monitor

the $ denotes an existing variable, not a new one.
 
Old 04-27-2004, 01:18 PM   #3
hw-tph
Senior Member
 
Registered: Sep 2003
Location: Sweden
Distribution: Debian
Posts: 3,032

Rep: Reputation: 57
Do you want to assign the contents of $mypcp to $monitor, is that it?
Code:
mypcp="192.168.254.254"
monitor=$mypcp
echo $monitor
You need to set $mypcp before setting $monitor (you can set a variable several times in a script if you wish) since if you do it the other way around, $mypcp will not contain anything when you try to set $monitor to the contents of $mypcp...did that make any sense?


Håkan
 
Old 04-27-2004, 01:33 PM   #4
mikshaw
LQ Addict
 
Registered: Dec 2003
Location: Maine, USA
Distribution: Slackware/SuSE/DSL
Posts: 1,320

Rep: Reputation: 45
I don't think that's exactly what sdandeker wanted. Your solution would make the values of $monitor and $mypcp identical. It seems the result should be that the value of one variable becomes the _name_ of the other variable.
Thinking about it is beginning to make my head spin though (not enough coffee)
 
Old 04-27-2004, 03:58 PM   #5
hw-tph
Senior Member
 
Registered: Sep 2003
Location: Sweden
Distribution: Debian
Posts: 3,032

Rep: Reputation: 57
Mik, you think he wants $monitor to contain the string "$mypcp", not at all related to the variable $mypcp? If so then monitor=\$mypcp; echo $monitor would return the string $mypcp and not the contents of the actual variable $mypcp.

I've had my share of coffee today. Not enough beer, perhaps?


Håkan
 
Old 04-27-2004, 10:28 PM   #6
mikshaw
LQ Addict
 
Registered: Dec 2003
Location: Maine, USA
Distribution: Slackware/SuSE/DSL
Posts: 1,320

Rep: Reputation: 45
That's what I was guessing, but looking back at the original post I'm not sure.
the "Doing a echo "$"$monitor"" gives $mypcp" thing is what had me wondering
 
Old 04-28-2004, 02:35 AM   #7
sdandeker
LQ Newbie
 
Registered: Sep 2003
Location: UK
Distribution: Gentoo
Posts: 22

Original Poster
Rep: Reputation: 15
Youve all gotten me confused as to what I wanted to do!

Ok. Lets start from the beginning.

I have:

mypc1p=192.168.0.1
mypc2p=192.168.0.2

for monitor in mypc1 mypc2
do

ping -c 3 $monitor>/tmp/$monitor
...
read $monitor</tmp/$monitor

SO... I want to be able to do several things in my program by using the monitor variable to change the way the program behaves. The first time the loop runs, it should ping the 1st IP and put the result in a file which has "mypc1" in its name. I then use some grep to pull the average ping value and eventually want to assign this result to a variable which contains the name "mypc1".

This will basically allow me to use a for loop changing the filenames and variable names instead of writing the same code 3 times with different names.

Make Sense?
 
Old 04-28-2004, 03:34 AM   #8
AutOPSY
Member
 
Registered: Mar 2004
Location: US
Distribution: Redhat 9 - Linux 2.6.3
Posts: 836

Rep: Reputation: 31
Not really, Id say you are going about it wrong, if thats the pourpose of the script.
 
Old 04-28-2004, 04:45 AM   #9
XavierP
Moderator
 
Registered: Nov 2002
Location: Kent, England
Distribution: Lubuntu
Posts: 19,176
Blog Entries: 4

Rep: Reputation: 430Reputation: 430Reputation: 430Reputation: 430Reputation: 430
Moved: This thread is more suitable in Programming and has been moved accordingly to help your thread/question get the exposure it deserves.
 
Old 04-28-2004, 03:55 PM   #10
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
Re: calling a variable within a variable

Quote:
monitor="mypcp"
mypcp="192.168.254.254"

Now I want the result 192.168.254.254 using the variable monitor!
Try this:
Code:
monitor="mypcp"
mypcp="192.168.254.254"

echo ${!monitor}
 
  


Reply


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


Similar Threads
Thread Thread Starter Forum Replies Last Post
variable $- swan2925 Linux - Software 1 07-20-2005 01:51 AM
bash, how to get variable name from variable Duudson Programming 6 01-06-2005 04:38 PM
Variable in variable-name Ephracis Programming 1 11-17-2004 11:38 AM
How to get 'Window' variable from a 'Widget' variable bordel Programming 0 11-19-2003 03:19 AM
IP Variable name? yank_ Linux - Networking 5 02-16-2001 06:40 PM


All times are GMT -5. The time now is 04:27 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