ansanes
ansanes

Reputation: 117

Execute if condition using ssh to get exit value

I'm trying to execute this command on a remote computer using ssh.

ssh user@host "if [ $(ls -la /folder | wc -l) -eq 83 ]; then true; else false; fi;"

How can I make this part $(ls -la /folder | wc -l) to be executed on the remote computer instead of locally?

Upvotes: 0

Views: 166

Answers (1)

William Pursell
William Pursell

Reputation: 212198

To prevent $() from being expanded locally, put it in single quotes:

ssh user@host 'if [ $(ls -la /path | wc -l) -eq 83 ]; then true; else false; fi;'

But you don't need the if/else, just do:

ssh user@host '[ $(ls -la /path | wc -l) -eq 83 ]'

Upvotes: 1

Related Questions