ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Im trying to declare a variable that will contain the contents of another variable.
Code:
#!/bin/ksh
query1="select * from table1"
query2="select * from table2"
query3="select * from table3"
for i in 1 2 3
do
VAR=`echo query${i}`
echo ${VAR}
done
This is the basic idea of what im trying to do but instead of VAR being populated with the sql it contain query1,2 or 3.
How exactly do i get to stick the sql statement into another variable.
You have to use indirect reference through the eval var1=\$$var2 mechanism
Code:
#!/bin/ksh
query1="select * from table1"
query2="select * from table2"
query3="select * from table3"
for i in 1 2 3
do
eval VAR=\$query$i
echo "$VAR"
done
I'm not sure this is what you're looking for, anyway.
You have to use indirect reference through the eval var1=\$$var2 mechanism
Code:
#!/bin/ksh
query1="select * from table1"
query2="select * from table2"
query3="select * from table3"
for i in 1 2 3
do
eval VAR=\$query$i
echo "$VAR"
done
I'm not sure this is what you're looking for, anyway.
That is exactly what i am after, thank you very much.
can i ask what the eval VAR=\$query bit is actually doing.
Im trying to declare a variable that will contain the contents of another variable.
Code:
#!/bin/ksh
query1="select * from table1"
query2="select * from table2"
query3="select * from table3"
for i in 1 2 3
do
VAR=`echo query${i}`
echo ${VAR}
done
This is the basic idea of what im trying to do but instead of VAR being populated with the sql it contain query1,2 or 3.
How exactly do i get to stick the sql statement into another variable.
You want to do:
Code:
#!/bin/ksh
#
one=1
two=2
three=3
four=4
five=5
for DIGIT in one two three four five; do
nameref NUMBER=${DIGIT}
echo ${DIGIT} ${NUMBER}
done
No, no, no! This is not the correct way to do it at all. This is a classic case of a situation where an array* should be used.
Also, you need to make sure your parameters are properly quoted to keep the "*" globbing character from expanding to a list of filenames in the current directory.
Code:
#!/bin/ksh
query[1]='select * from table1'
query[2]='select * from table2'
query[3]='select * from table3'
for i in 1 2 3; do
VAR=${query[i]}
echo "$VAR"
done
*Why do people ignore arrays so often? Please forget all about trying to generate dynamic variable names; in the vast majority of cases you don't need them. If you have a list of things to access, store it in an array, and call it by number.
I agree with David the H. that for simple cases of lists like this, arrays are usually the best solution. If numbers are not desired, associative arrays (hashes in most newer perl documentation) might be used. However, it is useful to know alternate solutions, as well as other uses for namerefs. Had it not been for this thread, I think I might not have found out about this from the ksh man page:
Quote:
A nameref provides
a convenient way to refer to the variable inside a function whose name
is passed as an argument to a function ...
The ironic thing is, the same shells that have neat features like that for easily handling indirect references are also the ones that offer other features that make using them generally unnecessary. And vice-versa.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.