Catching error in sftp

Hi All Experts,

I have a script which logs to the server via sftp connection with below code :-

user\_name@sftp_server.com

and the connection is going smooth.

My requirement is to place file in sftp_server in some path.

and if path doesn't exist or the file is not put successfully I need to raise an exception.

Below is the code which I am using to accomplish my requirement but the error for path doesn't exist is not getting captured.

FTP2NT(){
  FILE_INPUT=$1;
  USER1=$2;
  SERVER=$3;
  PATH1=$4;
  sftp $USER1@$SERVER <<ENDofFTP
  cd $PATH1
  put $FILE_INPUT
  exit
ENDofFTP
}


FTP2NT $zipfile $user $sftpserver $path
sftp_status=$?
if [ "$sftp_status" = "0" ]
then
  echo "Hi \n \t SFTP was successful for $DATE_SEND files." | mailx -s "SFTP success" $mlist
else
  echo "sftp was not successful"
  echo "Hi \n \t SFTP was not successful. Kindly check the script $0 @ `hostname`." | mailx -s "SFTP process failed" $errorlist
fi
done

you need to redirect the output of sftp and grep the known errors in the output file

Hi itkamaraj,

The error which comes if path doesn't exist is as below...

sftp > Couldn't canonicalise: No such file or directory

is there a way if we can capture all sorts of error ?

sftp $USER1@$SERVER <<ENDofFTP 2>>/ab/cd/yourErrorfile