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")
You're missing
;
s. The general syntax if you put it all in one line would be:The
thing
can be pretty much anything that returns an exit code. Thethen
branch is taken if thatthing
returns 0, theelse
branch otherwise.[
isn't syntax, it's thetest
program (check outls /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:Can't get much simpler than this :)
one line, proper quoting
Test if a file exists:
And the opposite, test if a file does not exist:
On CentOS machine, the oneliner bash that worked for me was:
It needed I/O redirection (as the top answer) as well as quotes around the command to be run on remote.
Here is a simple approach: