SFTP file and archive only if its succedded

Hi All,

I have a requirement where I have to archive the file as soon as I sFTP the file to remote location

below is the code I am going with

ls $FILE_NAME | while read FNAME
do
sftp -v -oport=nn $FTP_USER@$FTP_HOST <<EOF 2>&1 | tee -a ${LOG_FILE_NAME}
cd ${TGT_FOLDER_NAME}
lcd ${SRC_FOLDER_NAME}
put ${FNAME}
EOF
mv ${SRC_FOLDER_NAME}/$FNAME $TGT_ARCH
if [ $? -ne 0 ];then
  echo "Archiving failed  please check the log " | tee -a  ${LOG_FILE_NAME}
  FLAG=3
  exit $FLAG
else
  echo "Archiving succeded" for $FILE_NAME | tee -a  ${LOG_FILE_NAME}
fi
done

My question is how to make sure that the ftp is successful so I can go with archiving

Capture sftp exit code:

ls $FILE_NAME | while read FNAME
do
sftp -v -oport=nn $FTP_USER@$FTP_HOST <<EOF 2>&1 | tee -a ${LOG_FILE_NAME}
cd ${TGT_FOLDER_NAME}
lcd ${SRC_FOLDER_NAME}
put ${FNAME}
EOF
if [ $? -ne 0 ];then
   echo "sftp fails"
   exit
fi 
mv ${SRC_FOLDER_NAME}/$FNAME $TGT_ARCH
if [ $? -ne 0 ];then
  echo "Archiving failed  please check the log " | tee -a  ${LOG_FILE_NAME}
  FLAG=3
  exit $FLAG
else
  echo "Archiving succeded" for $FILE_NAME | tee -a  ${LOG_FILE_NAME}
fi
done

Sorry its not working

I have ran the code in an environment where the sFTP connection is not set up and it didnt ftped the file although the file is archived

Remove tee and try again:

sftp -v -oport=nn $FTP_USER@$FTP_HOST <<EOF 2>&1
cd ${TGT_FOLDER_NAME}
lcd ${SRC_FOLDER_NAME}
put ${FNAME}
EOF
if [ $? -ne 0 ];then
   echo "sftp fails"
   exit
fi

I guess you're getting tee exit code.
To get status from both commads check PIPESTATUS: Internal Variables

Thanks ,it worked