user3809938
user3809938

Reputation: 1304

What is wrong with this unix command when i try it via ssh?

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

Answers (1)

Jon
Jon

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

Related Questions