-1

I have a shell script where I am executing ls command from different hosts within it but I am having trouble achieving this. Here is my colde below:

#!/bin/sh      
nodes="hostname1 hostname2" 
cmd="ls -ltrha"
for node in ${nodes}; do
    ssh ${node} '`$cmd`'
done

The problem with this code is that it can ssh hostname1 perfectly but it doesn't even execute the ls command or even ssh hostname2.

Any idea what I am doing wrong here?

****New Edit**** I also tried the below one liner, but it only executed the ls without the ssh

     ssh hostname1 'ls -ltrha'
Dr.Bake
  • 141
  • 4
  • 18
  • See: [Difference between single and double quotes in bash](http://stackoverflow.com/q/6697753/3776858) – Cyrus Jul 30 '17 at 11:32
  • Thank you for your comment Cyrus but I don't think the issue is just with quotes. I just tried running a one liner and instead it only excuted the $cmd without executing the ssh commend. Please take a look at my post – Dr.Bake Jul 30 '17 at 11:35
  • please update your Q to indicate how you are dealing with the `password` prompt from the remote host. Good luck. – shellter Jul 30 '17 at 12:53

2 Answers2

0
#!/bin/sh      
nodes="hostname1 hostname2"
cmd="ls -ltrha"
for node in $nodes; do
    ssh "$node" "$cmd"
done
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
-1

you can do it

#!bin/sh
nodes="hostname1 hostname2" 
for node in ${nodes}; do
ssh ${node} 
`ls -ltrha`
done
summ
  • 149
  • 2
  • 5