bash: check if remote file exists using scp -


I am writing a Bash script to copy a file from a remote script into my local machine. I have to check to see whether the file is available or not, so I can take an alternative action if it is not.

I know if a local file exists or not, check it, however, using SCP makes things complicated by bit. General knowledge tells me that in a way the file will be attempted to skip in any way, and check the return code from the scp command. Is this the right way to go about it?

If so, how do I check the return code from the SCP invitation?

Using some shell code embedded in the CSD + CMD line; Use this method when you need to make a decision before file transfer fails; SH Remote-host 'sh -c' if [-f / / myfile]; Then gzip -c ~ / myfile; Fi '' | Gzip -dc & gt; /tmp/pkparse.py/spy/pkparse.py/en/default/

If you want to transfer directories, you may want to "tar" - first

if you want to use scp So you can check return code like this:

  if scp remote-host: ~ / myfile ./ & gt; & Amp; / Dev / null; Then echo the "OK" transfer; The second "Transfer failed" echo; Fi   

This really depends on when it is important for you to know whether the file is or not; Before the transfer begins (use ssh + sh) or after its expiry

Comments