Search code examples
sshsh

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?


Solution

  • 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 ]'