![]() |
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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 |
|
||||
|
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.. |
|
||||
|
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 |
![]() |
| Bookmarks |
| Tags |
| dynamic, eval, variables |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|