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 04-29-2014, 12:10 PM   #1
Martin Peter Clarke
LQ Newbie
 
Registered: Nov 2007
Posts: 12

Rep: Reputation: 0
Linux: variable within variable


Hi, this is pathetic I know:

CUSTOMER_REFS_ZZ="ZZ ZEE ZULU ZEBRA"
echo $CUSTOMER_REFS_ZZ
ZZ ZEE ZULU ZEBRA

CUSTOMER_SHORT_NAME="ZZ"
echo $CUSTOMER_SHORT_NAME
ZZ

CUSTOMER_REFS="CUSTOMER_REFS_$CUSTOMER_SHORT_NAME"
echo $CUSTOMER_REFS
CUSTOMER_REFS_ZZ

echo $CUSTOMER_REFS_ZZ
ZZ ZEE ZULU ZEBRA

I would like a one liner something like

echo $`echo $CUSTOMER_REFS`

to give

ZZ ZEE ZULU ZEBRA

but all it gives is

$CUSTOMER_REFS_ZZ

i.e. I don't want to refer to the 'inner' $CUSTOMER_REFS_ZZ variable directly.

Help would be MUCH appreciated. Name your single malt!

Martin
 
Old 04-29-2014, 12:11 PM   #2
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
You're looking for "eval"

http://www.tldp.org/LDP/abs/html/ivr.html
 
1 members found this post helpful.
Old 04-29-2014, 12:19 PM   #3
Martin Peter Clarke
LQ Newbie
 
Registered: Nov 2007
Posts: 12

Original Poster
Rep: Reputation: 0
suicidaleggroll - THANK YOU!

Wow! It'll take me a while to follow that ... I am OLLLLDDD and dumb and need the bathroom and its 18:20 here already and then I HAVE to get home.

So bear with me not implementing it until tomorrow probably.

Kind regards - Martin
 
Old 04-29-2014, 12:25 PM   #4
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,780

Rep: Reputation: 2213Reputation: 2213Reputation: 2213Reputation: 2213Reputation: 2213Reputation: 2213Reputation: 2213Reputation: 2213Reputation: 2213Reputation: 2213Reputation: 2213
Do not use "eval". It is very dangerous to use unless you have checked that the strings do not contain any characters that are special to the shell. What you want is variable indirection:
Code:
$ CUSTOMER_REFS_ZZ="ZZ ZEE ZULU ZEBRA"
$ CUSTOMER_SHORT_NAME="ZZ"
$ CUSTOMER_REFS="CUSTOMER_REFS_$CUSTOMER_SHORT_NAME"
$ echo ${!CUSTOMER_REFS}
ZZ ZEE ZULU ZEBRA
 
1 members found this post helpful.
Old 04-29-2014, 12:34 PM   #5
Martin Peter Clarke
LQ Newbie
 
Registered: Nov 2007
Posts: 12

Original Poster
Rep: Reputation: 0
rknichols ... I haven't been to the bathroom but it was worth it! Thank you.
 
Old 04-29-2014, 12:38 PM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
If you want to know more about eval you can also look here

As it is also fraught with some dangers of what people could force your program to do if the know eval is around and not being used correctly, I generally try to stay clear.

My normal first question would be, what benefit do you get from being able to combine variable names in this way? (I also find it a little odd (funny) as well because when talking with programmers
in other languages this requirement rarely if ever actually comes up, yet many people try to do it with bash .. oh well)

Could your solution perhaps be solved using associative arrays instead? (see earlier in the page of the link above for details)
Code:
declare -A CUSTOMER_REFS

CUSTOMER_SHORT_NAME="ZZ"

CUSTOMER_REFS[$CUSTOMER_SHORT_NAME]="ZZ ZEE ZULU ZEBRA"

echo "${CUSTOMER_REFS[$CUSTOMER_SHORT_NAME]}"
 
1 members found this post helpful.
Old 04-29-2014, 01:08 PM   #7
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
Quote:
Originally Posted by rknichols View Post
Do not use "eval". It is very dangerous to use unless you have checked that the strings do not contain any characters that are special to the shell. What you want is variable indirection:
Code:
$ CUSTOMER_REFS_ZZ="ZZ ZEE ZULU ZEBRA"
$ CUSTOMER_SHORT_NAME="ZZ"
$ CUSTOMER_REFS="CUSTOMER_REFS_$CUSTOMER_SHORT_NAME"
$ echo ${!CUSTOMER_REFS}
ZZ ZEE ZULU ZEBRA
Yes it is dangerous IF you're getting the variable name from user input from users you don't trust, but you ALWAYS need to be careful when you're reading and using user input from users you don't trust, regardless of the language or what you're doing with the information. There's no need to jump into panic mode unless this is one of those situations.

Last edited by suicidaleggroll; 04-29-2014 at 01:09 PM.
 
1 members found this post helpful.
Old 04-29-2014, 08:23 PM   #8
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,780

Rep: Reputation: 2213Reputation: 2213Reputation: 2213Reputation: 2213Reputation: 2213Reputation: 2213Reputation: 2213Reputation: 2213Reputation: 2213Reputation: 2213Reputation: 2213
The issues are more than just deliberate malice. You have to absolutely sure of what can show up in those fields. You can have a script that runs fine, then perhaps a short name "AT&T" gets put in the database and the ampersand makes your "eval" do something strange. A string with an embedded single quote would also make your script fail, and protecting against these with quoting gets complicated because strings are being parsed twice by the shell. Using "eval" when a simpler, more direct, and much safer alternative is available is just misguided.
 
1 members found this post helpful.
Old 04-29-2014, 10:44 PM   #9
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
Quote:
Originally Posted by rknichols View Post
You have to absolutely sure of what can show up in those fields.
Same with any script. 99.9999% of the solutions to questions posted on this forum use syntax that would break down if an ampersand or quote made its way into a variable. You can't protect against everything.

Quote:
Originally Posted by rknichols View Post
You can have a script that runs fine, then perhaps a short name "AT&T" gets put in the database and the ampersand makes your "eval" do something strange. A string with an embedded single quote would also make your script fail
It would make both approaches fail...they would just fail in different ways. If an ampersand or quote gets into the variable that you're using to build up a dynamic variable name, the script is going to fail no matter what method you use to build that dynamic variable name.

Quote:
Originally Posted by rknichols View Post
Using "eval" when a simpler, more direct, and much safer alternative is available is just misguided.
I'd argue that this approach in general is misguided, and there is almost always a better, cleaner, and more robust way of attacking the problem than dynamically building up a variable name using other variables. If it MUST be done, then yes ${!var} (if it's available) is a better way of getting to the result than eval, but ${!var} is not always an option, and its use would reduce the code's portability.

Last edited by suicidaleggroll; 04-29-2014 at 10:48 PM.
 
1 members found this post helpful.
  


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
[SOLVED] ksh read variable from text file and populate with content of shell variable WindozBytes Programming 4 09-17-2012 01:48 PM
Need help with script writing: Storing cmd in variable, print variable, then exe cmds Arodef Programming 3 01-17-2012 12:26 AM
problem while comparing awk field variable with input variable entered using keyboard vinay007 Programming 12 08-23-2011 12:44 AM
AWK a variable Ouptut to a new variable and using the new variable with the old one alertroshannow Linux - Newbie 4 02-16-2009 12:08 AM

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

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