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_Development/Puffer_and_Traps/Repeaters_Network/UBC_LOGS/log1349544129.tar.bz2 ] then echo "okidoke"; else "not okay!" fi
-sh: syntax error: unexpected "else" (expecting "then")
I wanted also to check if a remote file exist but with RSH. I have tried the previous solutions but they didn't work with RSH.
Finally, I did I short function which works fine:
In addition to the answers above, there's the shorthand way to do it:
-q
is quiet mode, it will suppress warnings and messages.As @Mat mentioned, one advantage of testing like this is that you can easily swap out the
-f
for any test operator you like:-nt
,-d
,-s
etc...Test Operators: http://tldp.org/LDP/abs/html/fto.html
The above will run the echo command on the machine you're running the ssh command from. To get the remote server to run the command:
You can specify the shell to be used by the remote host locally.
And be careful to (single-)quote the variables you wish to be expanded by the remote host; otherwise variable expansion will be done by your local shell!
So, to check if a certain file exists on the remote host you can do the following: