1751805 Members
4868 Online
108781 Solutions
New Discussion юеВ

Re: shell substitution

 
SOLVED
Go to solution
Igor Sovin
Super Advisor

shell substitution

Hi!

Working on the following problem

I have script for example:
export rx1=hostname1
export rx2=hostname2
export rx3=hostname3
for i in 1 2 3
do
ssh $(rx$i) <- need to get here hostname1, hostname2 etc
.....
.....
done

How to do that?
i only get rx1 or rx2 or rx3 instead of hostname1, hostname2 or hostname3

thank you
9 REPLIES 9
T G Manikandan
Honored Contributor
Solution

Re: shell substitution

for i in hostname1 hostname2 hostname3
do
ssh $i

..
done

OR

make entries in file /tmp/a

hostname1
hostname2
hostname3

Then in the script

for i in `cat /tmp/a`
do
ssh $i
..
..
done

Igor Sovin
Super Advisor

Re: shell substitution

Shame on me! its so easy :)

thank you!
Paul_481
Respected Contributor

Re: shell substitution

Hi Igor,

Use array.

insert this line

rx=3
export rx[1]=hostname1
export rx[2]=hostname2
export rx[3]=hostname3

for i in 1 2 3
do
ssh ${rx[$i]}

.....
.....
done

Hope it helps.

Regards,
Paul
Igor Sovin
Super Advisor

Re: shell substitution

thank you guys!
Both methods are work!
Muthukumar_5
Honored Contributor

Re: shell substitution

You got the answer. You can use array's effectivly as,

#!/bin/ksh

export rx[1]=hostname1
export rx[2]=hostname2
export rx[3]=hostname3
let i=0

while [[ $i -lt ${#rx[*]} ]]
do
ssh ${rx[$i]}
let i=i+1
done

exit 0
##

HTH.
Easy to suggest when don't know about the problem!
Bill Hassell
Honored Contributor

Re: shell substitution

The construct $() has the results of a command execution. When you place just the variable inside the parens (), the shell tries to run the string rx1$hostname1 which is not what you want. Just remove the command substitution and it works fine:

export rx1=hostname1
export rx2=hostname2
export rx3=hostname3
for i in 1 2 3
do
ssh rx$i ...
done

Although the ${} curly braces are not needed in this context, it is always good script writing practice to explicitly separate variables from plaintext as in:

ssh rx${i} ...


Bill Hassell, sysadmin
Nguyen Anh Tien
Honored Contributor

Re: shell substitution

Hi logor
1. Make one file contains all hosts
#vi allhosts
hostname1
hostname2
hostname3
#vi yourscript
for i in `cat hosts`
do
ssh $i
done
Try it now
HTH
tienna
HP is simple
Bob_Vance
Esteemed Contributor

Re: shell substitution

A much simpler way to use the array method (which I like) is:


rx[1]=hostname1
rx[2]=hostname2
rx[3]=hostname3
export rx

for i in ${rx[@]}
do
echo ssh ${i}
done


This [@] is much more general and expands to the entire array list.
You don't have to do any counting or know how big the array is!!


HTH
bv
"The lyf so short, the craft so long to lerne." - Chaucer
Stuart Browne
Honored Contributor

Re: shell substitution

So basically you're wanting to do multi-level evaluation.

You first want to evaluate $i to be a digit, which is working.. You then want to re-evaluate the new string to get the contents of the next variable.. i.e.

rx$i => rx1

You then want to get:

$rx1 =>

but that second part wasn't working.

You can use one of the other solutions here, of which I'd highly reccomend.. But for the sake of the argument, there's usually a few ways to do the multi-level evaluation.

One is to use the 'eval' command:

ssh $(eval "echo \$rx$i")

The 'eval' command allows for the second leevl of evaluation. So the shell would work like this:

eval "echo \$rx$i" => echo $rx1 => hostname1

thus the answer you wanted.

Anyway, have fun :)
One long-haired git at your service...