LinuxQuestions.org
Visit Jeremy's Blog.
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 06-11-2008, 08:50 PM   #1
JimOrJoe
LQ Newbie
 
Registered: Dec 2007
Posts: 24

Rep: Reputation: 15
Shell scripting : how to name a variable with the value of another variable


Hi, there:

I have to name a new variable with the value of another variable, then give the new variable a value and export it. Could anyone please give me some sample shell scripts?

To clarify the scenario for those who did not comprehend the above description, here is the elaboration:

In a shell script, I received a argument, say varName. Its value is a random string. Let's take it for granted that the string is "slip", how do I delcare a variable using varName intead of scripting slip="pink"?

Thanks and Cheers

Joe
 
Old 06-11-2008, 08:59 PM   #2
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
man 1 eval
 
Old 06-11-2008, 09:27 PM   #3
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Use arrays instead, if you are using bash, or other shells that supports arrays.
 
Old 06-11-2008, 09:34 PM   #4
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
Quote:
Originally Posted by ghostdog74 View Post
Use arrays instead, if you are using bash, or other shells that supports arrays.
I don’t think the OP’s situation can be done with arrays (although I may be wrong). I think he or she wants a simple:
Code:
eval $varName=pink
Of course usually code which requires the use of eval may have potential for better overall design, but sometimes it’s convenient.
 
Old 06-11-2008, 10:12 PM   #5
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by osor View Post
I don’t think the OP’s situation can be done with arrays (although I may be wrong). I think he or she wants a simple:
Code:
eval $varName=pink
Of course usually code which requires the use of eval may have potential for better overall design, but sometimes it’s convenient.
just my thoughts unless i really misinterpret the requirement. Whether naming a variable with the name of another or not, eventually, all we want to do is to allocate memory, because variables are just memory locations. Using arrays is analogous (whatever that word's spelling is ) to that. So at first if var="slip", it could be a[0]="slip". then $var$Name="pink" , it could be a[1]="pink". although it would be nicer if the tool support associative arrays. $0.02
 
Old 06-11-2008, 10:15 PM   #6
JimOrJoe
LQ Newbie
 
Registered: Dec 2007
Posts: 24

Original Poster
Rep: Reputation: 15
with eval, I found out how to read from a variable with a random name like this:

foo=10 randomVarName=foo
eval y='$'$randomVarName
echo $y
10

The question is how to set value to the variable with a random name:

randomVarName=foo
eval y='$'$randomVarName
?
echo $y
what should i do to give a value to $foo at line "?"

Thanks

Last edited by JimOrJoe; 06-11-2008 at 11:07 PM.
 
Old 06-12-2008, 06:02 AM   #7
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Quote:
Originally Posted by JimOrJoe View Post
with eval, I found out how to read from a variable with a random name like this:

foo=10 randomVarName=foo
eval y='$'$randomVarName
echo $y
10
Another way (bash only):
Code:
foo=10
randomVarName=foo
y=${!randomVarName}
echo $y
Quote:
Originally Posted by JimOrJoe View Post
The question is how to set value to the variable with a random name
Code:
randomVarName=foo
eval $randomVarName=123
echo $foo
 
Old 06-12-2008, 06:38 AM   #8
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Thumbs up I R tips hat...

Quote:
Originally Posted by Hko View Post
Code:
y=${!randomVarName}
It's clean, utterly incompatible, I like that ;-p

Two evals goooohhhd, four evals baaahhhhd!
 
Old 06-12-2008, 10:08 AM   #9
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Quote:
Originally Posted by unSpawn View Post
It's clean, utterly incompatible, I like that ;-p
True, just like C++ is incompatible with Fortran.
:-)
 
Old 06-15-2008, 09:36 PM   #10
JimOrJoe
LQ Newbie
 
Registered: Dec 2007
Posts: 24

Original Poster
Rep: Reputation: 15
Thanks a lot mentors, you have answered my question
 
  


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
Bash Shell Scripting Dynamic Variable naming question ZuG Programming 2 02-07-2007 02:39 PM
Bash scripting: how can I reference a variable? frankie_DJ Programming 4 10-06-2006 05:13 AM
Scripting: accessing a variable stored in a variable? tomolesonjr Linux - Newbie 5 05-05-2006 08:47 PM
shell scripting - using ${variable:=","} Steve Riley Linux - General 7 02-08-2005 05:07 AM
shell scripting: variable problem XST1 Programming 7 01-30-2005 01:45 PM

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

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