Re: shell substitution (338 Views)
Reply
Super Advisor
Igor Sovin
Posts: 286
Registered: ‎07-08-2002
Message 1 of 10 (338 Views)
Accepted Solution

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
Honored Contributor
T G Manikandan
Posts: 5,533
Registered: ‎12-11-2000
Message 2 of 10 (338 Views)

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

Super Advisor
Igor Sovin
Posts: 286
Registered: ‎07-08-2002
Message 3 of 10 (338 Views)

Re: shell substitution

Shame on me! its so easy :)

thank you!
Respected Contributor
Paul_481
Posts: 253
Registered: ‎09-08-2004
Message 4 of 10 (338 Views)

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
Super Advisor
Igor Sovin
Posts: 286
Registered: ‎07-08-2002
Message 5 of 10 (338 Views)

Re: shell substitution

thank you guys!
Both methods are work!
Honored Contributor
Muthukumar_5
Posts: 4,030
Registered: ‎06-09-2004
Message 6 of 10 (338 Views)

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!
Honored Contributor
Bill Hassell
Posts: 14,225
Registered: ‎05-29-2000
Message 7 of 10 (338 Views)

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} ...
Honored Contributor
Nguyen Anh Tien
Posts: 923
Registered: ‎05-20-2004
Message 8 of 10 (338 Views)

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
Esteemed Contributor
Bob_Vance
Posts: 602
Registered: ‎06-13-2000
Message 9 of 10 (338 Views)

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
Honored Contributor
Stuart Browne
Posts: 3,032
Registered: ‎02-20-2002
Message 10 of 10 (338 Views)

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...
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.