LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 10-20-2008, 04:07 PM   #1
4play
LQ Newbie
 
Registered: Oct 2003
Location: london
Distribution: Centos
Posts: 27

Rep: Reputation: 15
korn shell variable help


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.
 
Old 10-20-2008, 04:20 PM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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.
 
Old 10-20-2008, 04:30 PM   #3
4play
LQ Newbie
 
Registered: Oct 2003
Location: london
Distribution: Centos
Posts: 27

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by colucix View Post
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.

why should i have to escape the first $ ?
 
Old 10-20-2008, 04:36 PM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
If you escape the first $ it will be interpreted literally from eval, that is you have to evaluate the following:
Code:
VAR=$query1
in the eval statement the first escaped $ will remain untouched, while the $i variable will be substituted by its value:
Code:
eval VAR=\$query$i
 
Old 10-20-2008, 04:40 PM   #5
4play
LQ Newbie
 
Registered: Oct 2003
Location: london
Distribution: Centos
Posts: 27

Original Poster
Rep: Reputation: 15
Thanks alot for that.

I have been bashing my head against that problem for about 4 hours today.
 
Old 11-23-2011, 01:44 AM   #6
allanf
Member
 
Registered: Sep 2008
Location: MN
Distribution: Gentoo, Fedora, Suse, Slackware, Debian, CentOS
Posts: 100
Blog Entries: 1

Rep: Reputation: 20
Quote:
Originally Posted by 4play View Post
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
Which results in:
Code:
one 1
two 2
three 3
four 4
five 5
 
Old 11-23-2011, 06:38 AM   #7
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 735

Rep: Reputation: 76
Hi.

Old thread, but possibly a useful note from man page for ksh:
Quote:
If a nameref is used as the index of a for loop, a name
reference is established for each item in the list.
See an example at post #4 at http://www.linuxforums.org/forum/pro...once-only.html ... cheers, makyo
 
Old 11-23-2011, 09:23 AM   #8
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Sigh...

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.
 
Old 11-23-2011, 09:39 AM   #9
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 735

Rep: Reputation: 76
Hi.

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 ...
Best wishes ... cheers, makyo

Last edited by makyo; 11-23-2011 at 09:43 AM.
 
Old 11-23-2011, 12:03 PM   #10
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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.

A good synopsis on indirect referencing in both bash and ksh can be found here:
http://mywiki.wooledge.org/BashFAQ/006
 
  


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
How to parse in korn shell eddieboo Linux - Newbie 1 04-19-2008 07:06 AM
korn shell loop jonlake Programming 2 03-28-2008 12:10 PM
Korn Shell equivalent bassplayer69 Linux - General 1 08-02-2007 01:15 PM
script in korn shell disruptive Programming 15 02-22-2007 10:18 AM
installing korn shell Synric Slackware 3 09-10-2006 08:57 AM

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

All times are GMT -5. The time now is 07:44 PM.

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