Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ssh command several variables 1

Status
Not open for further replies.

nabana2

Technical User
Sep 26, 2005
21
ZA
Hi

I am trying to return more than one result from a ssh command.

[root@server ~]# cmd=`for i in 1 2 3; do find /root/dir$i -type f | wc -l; done`
[root@server ~]# echo $cmd
27 5 8

[root@server ~]# cmd=`ssh 192.168.0.1 "for i in 1 2 3; do find /root/dir$i -type f | wc -l; done"`
root@192.168.0.1's password:
[root@server ~]# echo $cmd
8 8 8

I can solve it like this but I'd rather do it all in one connection:

[root@server ~]# cmd=`for i in 1 2 3; do ssh 192.168.0.1 "find /root/dir$i -type f | wc -l"; done`
root@192.168.0.1's password:
root@192.168.0.1's password:
root@192.168.0.1's password:
[root@server ~]# echo $cmd
27 5 8

Thanks
 
You may try this:
cmd=`ssh -f 192.168.0.1 "for i in 1 2 3; do find /root/dir$i -type f | wc -l; done"

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
PHV missed a final backquote:

[tt]cmd=`ssh -f 192.168.0.1 "for i in 1 2 3; do find /root/dir$i -type f | wc -l; done"[red]`[/red][/tt]

I prefer the $(cmd) syntax
[tt]cmd=$(ssh -f 192.168.0.1 "for i in 1 2 3; do find /root/dir$i -type f | wc -l; done")[/tt]


HTH,

p5wizard
 
Thanks PHV but i get the same result.
All values are set to the last returned result.

8 8 8 instead of 27 5 8
 
Thanks p5wiz.
Still no luck.
There is something wrong with the command as it does not return the correct results.

To return 1 value is fine but it seems to lose the others in the for loop.




 
And this ?
cmd=`ssh 192.168.0.1 "for i in 1 2 3; do find /root/dir$i -type f | wc -l >> /tmp/$$; done; cat /tmp/$$"`

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Use single quotes (with double quotes, your local shell is substituting the $i instead of sending the string $i over to the remote server):

cmd=`ssh -f 192.168.0.1 [red]'[/red]for i in 1 2 3; do find /root/dir$i -type f | wc -l; done[red]'[/red]`

or

cmd=$(ssh -f 192.168.0.1 [red]'[/red]for i in 1 2 3; do find /root/dir$i -type f | wc -l; done[red]'[/red])


HTH,

p5wizard
 
Thanks for explaining that p5wiz.

I decided to do this instead so that i could use some variables inside the quotes:

cmd=$(ssh 192.168.0.1 "for i in $d1 $d2 $d3; do find /root/dir\$i -type f \( -name "$pre*" \) -printf %f\\\n | wc -l; done")

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top