check if file exists on remote host with ssh

后端 未结 11 947
醉酒成梦
醉酒成梦 2021-01-31 03:20

I would like to check if a certain file exists on the remote host. I tried this:

$ if [ ssh user@localhost -p 19999 -e /home/user/Dropbox/path/Research_and_Devel         


        
11条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-31 03:31

    You're missing ;s. The general syntax if you put it all in one line would be:

    if thing ; then ... ; else ... ; fi
    

    The thing can be pretty much anything that returns an exit code. The then branch is taken if that thing returns 0, the else branch otherwise.

    [ isn't syntax, it's the test program (check out ls /bin/[, it actually exists, man test for the docs – although can also have a built-in version with different/additional features.) which is used to test various common conditions on files and variables. (Note that [[ on the other hand is syntax and is handled by your shell, if it supports it).

    For your case, you don't want to use test directly, you want to test something on the remote host. So try something like:

    if ssh user@host test -e "$file" ; then ... ; else ... ; fi
    

提交回复
热议问题