I have a script, which connecting to remote server and first checks, if the files are there by timestamp. If not I want the script exit without error. Below is a code
TARFILE=${NAME}.tar
TARGZFILE=${NAME}.tar.gz
ssh ${DESTSERVNAME} 'cd /export/home/iciprod/download/let/monthly;
Today=`date +%Y%m%d`;
files=`ls *${Today}* | wc -l`
echo "FILES ${files}">>${LOGF}
if [ ${files} = 0 ];then
echo "We have no monthly file ${Today}. ${NAME}.sh is exiting"
exit 0
else
find . -ctime -1 | tar -cvf transfer_dmz_start_monthly.tar *${Today *.*;
if [ $? != 0 ]
then
echo "Cannot create a tar file, the terminated abnormally"
exit 1
fi
gzip transfer_dmz_start_monthly.tar
fi'
if [ $? != 0 ]
then
echo "Something is wrong with connection to ${DESTSERVNAME} or ${MONTH_DEST_DIR}. Please check the connecttion to ${DESTSERVNAME} or files on ${MONTH
_DEST_DIR}. If there is only *tar file, delete it.">>${LOGF}
echo "!!!!! Alert!!!! Please check ${LOGF} and act accordingly. Please contact xxxx@xxxx.db.com. ${NAME}.sh terminated abnormaly" | mailx -r xxxx.xxxxxxxx@db.com -s "Script ${NAME}.sh failed" xxxx.xxxxxxxx@db.com
exit 1
fi
echo "${TARGZFILE} file was created successfuly ,starting to scp from ${DESTSERVNAME},then unzip and untar ${TARGZFILE}" >> $LOGF
echo >>${LOGF}
echo >>${LOGF}
#scp zipped file to DTE server
/usr/bin/scp ${DESTSERVNAME}:${MONTH_DEST_DIR}/${TARGZFILE} ${LOCL_FILES_DIR}/cool_${Today}/monthly
if [ $? != 0 ]
then
echo "Cannot scp ${TARGZFILE} file from ${DESTSERVNAME}. Please check if all files are on ${MONTH_DEST_DIR}/monthly.">>${LOGF}
echo "!!!!!!Alert!!!!!Please check ${LOGF} and act accordingly.Please contact xxxxx@xxxx.db.com. ${NAME}i.sh terminated abnormaly" | mailx -r xxxx.xxxxxxxx@db.com -s "Script ${NAME}.sh failed" xxxx.xxxxxxxx@db.com
exit 1
fi
The problem is that exit (which I put in read bold) exit only from remote server and continue run program, until scp tar file. It fails because we don't create it.
Could you please advice me to make a logic to exit from the script, if there are no files on remote server?
Thanks for contribution
---------- Post updated at 11:15 AM ---------- Previous update was at 10:25 AM ----------
I found solution.
Thanks