Register
It is currently Fri Sep 19, 2014 5:53 pm

Help Requested regarding indirect acces of variable


All times are UTC - 6 hours


Post new topic Reply to topic  [ 5 posts ] 
Author Message
 PostPosted: Thu May 26, 2011 12:27 pm   

Joined: Thu May 26, 2011 12:12 pm
Posts: 2
Hi all,
I am stuck :-/ ....!!
Question:

how to read value of a variable when a part of the name of variable is a another variable!!?

I need to access a variables set by different process with common name of variable with indexed extension.

for example:

"x" is a env variable set by process.
i=1 (i use for loop to increment from 1 to all possible values for example 5)
x1=5577
x2=5578
x3=5579
.....
can i use echo $x$i to get the value of 5577, 5578 ... ???

thanks in advance for any suggestion,

cheers,
Mirza


Top
 Profile  
 PostPosted: Thu May 26, 2011 1:42 pm   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 546
Hi,

already seen here:
Code:
x1=5577
x2=5578
x3=5579
for i in {1..3}; do other="x$i"; echo "${!other}"; done
you can ask web search engine for 'indirection'


Top
 Profile  
 PostPosted: Thu May 26, 2011 1:44 pm   

Joined: Thu May 26, 2011 12:12 pm
Posts: 2
Dear Watael,
thanks a lot for your reply. yes its exactly what i was looking for..... got it
thanks once again
cheers,
Mirza


Top
 Profile  
 PostPosted: Thu May 26, 2011 2:09 pm   
User avatar

Joined: Sun Jun 27, 2010 12:57 am
Posts: 192
Personally I'd go for
Code:
for i in {1..3}; do eval echo \$x$i; done
But that must be a matter of opinion :)


Top
 Profile  
 PostPosted: Thu May 26, 2011 4:51 pm   
User avatar

Joined: Tue Apr 27, 2010 2:28 pm
Posts: 172
Location: Czech Republic
Also, if you are using just numbers as the changing part of variable names, you might find arrays more suitable:
Code:
x=([1]=5577 5578 5579)
for i in {1..3}; do echo ${x[i]}; done


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP