Referencing variable using another variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Referencing variable using another variable
# 1  
Old 06-18-2008
Referencing variable using another variable

Hello, I am new to shell scripting so this might be a simple question...also, please excuse me if this is a topic discussed before.

I have declared a bunch of variables as such:
Host_1=32
Host_2=33
Host_3=34
temp=1
numUsers=4

Now, I am trying to go through a loop to access each of these variables. I have established a while loop like so:

while [ $temp -le numUsers]
do
echo "Host 1 = $Host_"$temp""
temp='expr $temp + 1'
done

Now this obviously is incorrect, as it looks for a variable called Host_ and doesn't find it, leaving me with only the value of temp being printed. Is there a way to have it so Host_1 is printed with temp=1? I looked into using an array but I figure there might be a quick solution to this.
# 2  
Old 06-18-2008
The shell supports arrays example for korn shell:
assume host_1 host_2 and host_3 are defined -
Code:
set -A arr $host_1 $host_2 $host_3
let tmp=0
while [[ $tmp -lt ${#arr[@]} ]]
do
       echo ${arr[tmp]}
       tmp=$(( tmp +1 ))
done

to create an array in bash use declare -a arrayname_goes_here
# 3  
Old 06-18-2008
I tried your code and it runs fine until ' echo ${arr[tmp]} '

I am using the standard shell script though. Are arrays not supported in it or is there something else I am overlooking? I am getting a bad substitution error by the way.
# 4  
Old 06-18-2008
I found the way I originally intended in another thread after further searching.
I just needed to use

eval echo " \$Host_$temp".

Your response was appreciated.
# 5  
Old 06-18-2008
If you are using ksh93, rather thanusing eval, you can use name references
Code:
#!/usr/bin/ksh93

Host_1=32
Host_2=33
Host_3=34

numUsers=4

for ((temp=1; temp < numUsers; temp++))
do
    typeset -n x=Host_$temp
    print $x
done

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

[Solved] How to increment and add variable length numbers to a variable in a loop?

Hi All, I have a file which has hundred of records with fixed number of fields. In each record there is set of 8 characters which represent the duration of that activity. I want to sum up the duration present in all the records for a report. The problem is the duration changes per record so I... (5 Replies)
Discussion started by: danish0909
5 Replies

2. Red Hat

How to pass value of pwd as variable in SED to replace variable in a script file

Hi all, Hereby wish to have your advise for below: Main concept is I intend to get current directory of my script file. This script file will be copied to /etc/init.d. A string in this copy will be replaced with current directory value. Below is original script file: ... (6 Replies)
Discussion started by: cielle
6 Replies

3. Shell Programming and Scripting

Variable not found error for a variable which is returned from stored procedure

can anyone please help me with this: i have written a shell script and a stored procedure which has one OUT parameter. now i want to use that out parameter as an input to the unix script but i am getting an error as variable not found. below are the unix scripts and stored procedure... ... (4 Replies)
Discussion started by: swap21783
4 Replies

4. Shell Programming and Scripting

Not able to store command inside a shell variable, and run the variable

Hi, I am trying to do the following thing var='date' $var Above command substitutes date for and in turn runs the date command and i am getting the todays date value. I am trying to do the same thing as following, but facing some problems, unique_host_pro="sed -e ' /#/d'... (3 Replies)
Discussion started by: gvinayagam
3 Replies

5. Shell Programming and Scripting

Variable Referencing

I am referencing variables in the following way var1="greeting" greeting="Welcome!" How do I echo var1 in such a way that it outputs Welcome! ? (3 Replies)
Discussion started by: milo7
3 Replies

6. Shell Programming and Scripting

How to find out which jobs are referencing my AutoSys variable?

I need to list out all the AutoSys jobs that are referencing a particular AutoSys global variable. I know the autorep command will definitely need to be used here, and maybe grep as well, but I can't figure out exactly how it's to be done. For example, "autorep -q -J %jobnamewildcard% | grep... (1 Reply)
Discussion started by: dazzwater
1 Replies

7. Shell Programming and Scripting

Split variable length and variable format CSV file

Dear all, I have basic knowledge of Unix script and her I am trying to process variable length and variable format CSV file. The file length will depend on the numbers of Earnings/Deductions/Direct Deposits. And The format will depend on whether it is Earnings/Deductions or Direct Deposits... (2 Replies)
Discussion started by: chechun
2 Replies

8. Shell Programming and Scripting

How to define a variable with variable definition is stored in a variable?

Hi all, I have a variable say var1 (output from somewhere, which I can't change)which store something like this: echo $var1 name=fred age=25 address="123 abc" password=pass1234 how can I make the variable $name, $age, $address and $password contain the info? I mean do this in a... (1 Reply)
Discussion started by: freddy1228
1 Replies

9. Shell Programming and Scripting

Insert a line including Variable & Carriage Return / sed command as Variable

I want to instert Category:XXXXX into the 2. line something like this should work, but I have somewhere the wrong sytanx. something with the linebreak goes wrong: sed "2i\\${n}Category:$cat\n" Sample: Titel Blahh Blahh abllk sdhsd sjdhf Blahh Blah Blahh Blahh Should look like... (2 Replies)
Discussion started by: lowmaster
2 Replies

10. Shell Programming and Scripting

Sed variable substitution when variable constructed of a directory path

Hello, i have another sed question.. I'm trying to do variable substition with sed and i'm running into a problem. my var1 is a string constructed like this: filename1 filerev1 filepath1 my var2 is another string constructed like this: filename2 filerev2 filepath2 when i do... (2 Replies)
Discussion started by: alrinno
2 Replies
Login or Register to Ask a Question