The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
google unix.com



UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to convert byteArray variables to HexaString variables for Linux? ritesh_163 High Level Programming 2 08-11-2008 12:55 AM
Dynamic variables within shell script isingh786 Shell Programming and Scripting 2 01-25-2007 09:44 AM
how to dynamic DNS bondoq Linux 1 11-14-2006 10:34 AM
Dynamic DNS [MA]Flying_Meat OS X (Apple) 0 12-06-2005 09:07 PM
dynamic pid? yls177 UNIX for Dummies Questions & Answers 2 12-17-2002 08:26 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 09-02-2008
max_payne1234 max_payne1234 is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 7
dynamic variables

I am new to unix and the following problem is bugging me.

var1="hello1"
var2="hello2"
var3="hello3"
counter=1
while [ $counter -le 3]
do

echo $var$counter

done

the idea here is to display the value of "var" based on the counter.
I am using the korn shell. I used array here but the problem is that I am executing the script from another script and so the prompt comes as "-A bad option"
Can the above problem be done in another way i.e without using arrays.
Can eval be useful here? please help.
code snippet for this would be very helpful

TIA
  #2 (permalink)  
Old 09-02-2008
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,346
Quote:
Originally Posted by max_payne1234 View Post
Can eval be useful here?
eval is useful here and this sample shows how it works:


Code:
#!/bin/sh

var1="hello1"
var2="hello2"
var3="hello3"
counter=1

while [ $counter -le 3 ]
do
  eval "eval_var=\$var${counter}"
  echo ${eval_var}
  let counter=counter+1
done

The backslash before the "$" sign in the eval command is to prevent the shell to expand the variable.

Regards

Last edited by Franklin52; 09-02-2008 at 02:04 PM..
  #3 (permalink)  
Old 09-02-2008
fpmurphy's Avatar
fpmurphy fpmurphy is offline Forum Staff  
Moderator
  
 

Join Date: Dec 2003
Location: Florida
Posts: 1,945
If you are using Kornshell 93 (ksh93) instread of ksh88 or pdksh, the following works

Code:
#!/bin/ksh93

var1="hello1"
var2="hello2"
var3="hello3"

for (( counter=1; counter <= 3; counter++ ))
do
   nameref myvar="var"${counter}
   print $myvar
done

  #4 (permalink)  
Old 09-02-2008
Andrek Andrek is offline
Registered User
  
 

Join Date: Aug 2006
Posts: 122
try this method, This way you can forget about hard coding your numeric of "3" so you know where to stop on your loop.

This adds the ability to add as many dymanic vars as you want.

Enjoy!

# You can set the array in two ways
# Method 1
# hellos="hello1 hello2 hello3"
# set -A myvar $hellos

#Method two
# myvar[0]="hello1"
# myvar[1]="hello2"
# myvar[2]="hello3"

# if your useing only one array then use what you
# feel is best for you.
# Method two offerres the option of adding more arrays
# that have a direct association to each other
# for example:

myvar[0]="hello1" ; othervar[0]="Bob"
myvar[1]="hello2" ; othervar[1]="Joe"
myvar[2]="hello3" ; othervar[2]="Mary"


count=0

# by using the ${#myvar[*]} you can add as many
# "myvars" and "othervars" as you want with out
# the need to rememdber the modify the max counter
# thus knowing where to end your loop.

while [ $count -lt ${#myvar[*]} ]
do
echo "${myvar[${count}]} - ${othervar[${count}]}"
let count=$count+1
done
  #5 (permalink)  
Old 09-05-2008
max_payne1234 max_payne1234 is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 7
thanks for the reply....igot one more solution .. to use eval

var=var$counter
eval var =\$$var

it works fine
Closed Thread

Bookmarks

Tags
dynamic, eval, variables

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 02:25 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0