Pls Help: SFTP Error Handling for transfers

I am working on a shell script where after making sftp connection to a remote server the file are being transferred. The problem is how to capture return code for the file which is missing at the remote location. I have tried to capture the return code which return value of "0" even the transfer of the file was unsuccessful.

Connecting to XX.XXX.XXX.XXX...
sftp> lcd /Z02/apps/output/CMBATCH/incoming
sftp> cd apl/download
sftp> get DATFILE.DAT
Couldn't stat remote file: No such file or directory
File "apl/download/DATFILE.DAT" not found.
sftp> bye

Could someone please suggest the best way to handle exception in this scenario?

Regards,

I would check for the presence of datfile.dat on the local system after the sftp session is complete.

rm datfile.dat
sftp remote_host
>get datfile.dat
>quit
if [ ! -r datfile.dat ]
then
  echo datfile.dat not present
fi
1 Like

Thanks JGT !! I can try that way also.

Does that mean SFTP doesn't support return codes for the file not found at remote location ?

Regards,

You could try something like:

if sftp remote_host <<-EOF
	cd apl/download
	get DATFILE.DAT
	bye
EOF
then	echo 'download successful'
else	echo 'download failed'
fi
1 Like

Appreciated.

So we can handle it once the transfer is over and check local location.

Thanks

Regards,