Reputation: 1304
When i run the below command:
ssh $HOST1 for dir in /local/apps/*; do ls -lrt $dir | grep live ; done
I get the below error:
bash: syntax error near unexpected token `do'
It works fine without ssh when doing locally.
I even tried
ssh $HOST1 "for dir in /local/apps/*; do ls -lrt $dir | grep live ; done"
But this gave an unexpected result. What is the correct format for doing a for loop via ssh?
Upvotes: 0
Views: 41
Reputation: 3671
Try
ssh $HOST1 'for dir in /local/apps/*; do ls -lrt $dir | grep live ; done'
With double quotes, that $dir
will be being expanded on the local machine, not as part of the remote SSH command.
Just for reference, the reason
ssh $HOST1 for dir in /local/apps/*; do ls -lrt $dir | grep live ; done
doesn't work is because it is interpreted as
ssh $HOST1 'for dir in /local/apps/*'
do ls -lrt $dir | grep live
done
with the for
running under SSH on the remote machine, and do
and done
running locally.
Upvotes: 3