Need help to handle errors during SFTP

Hi,

I have written a script that sftp's files from linux box to a IBM machine.

Can anybody help me how to handle any errors during SFTP file transfer.

I am writing the SFTP commands onto a file and executing that as you can see below.

while read line
do
if [[ -f ${line}".dat" ]] && [[ -f ${line}".trg" ]]; then
   echo "put "${line}".dat DMP.UPLOAD.DC4680."${line} >> sftp_queue.file
else
   echo ${line}" : no trg file associated"
fi
done < /red/wh4d/les/files/host/project/sftp.uniq >> /red/wh4d/les/files/host/sftp/sftp_log.$datetime

echo "bye" >> sftp_queue.file

sftp -b /red/wh4d/les/files/host/sftp_queue.file LIDIS@abc.light.com

Please suggest how i can get this done

how about:

if ! sftp -b /path/to/file 2> sftp-errlog
then
        echo "errors during transfer:" >>/path/to/errorlog
        cat sftp-errorlog >> /path/to/errorlog
fi
rm -f sftp-errorlog

But you don't need to write to a temp file with lots and lots of separate >> redirects. Redirect larger blocks.

(
        while read line
        do
        if [[ -f ${line}".dat" ]] && [[ -f ${line}".trg" ]]; then
           # stdout goes into sftp_queue.file
           echo "put "${line}".dat DMP.UPLOAD.DC4680."${line}
        else
           # stderr goes into /red/........./sftp_log.$datetime
           echo ${line}" : no trg file associated" >&2
        fi
done 

echo "bye" ) < /red/wh4d/les/files/host/project/sftp.uniq 2>> /red/wh4d/les/files/host/sftp/sftp_log.$datetime >> sftp_queue.file

if ! sftp -b /red/wh4d/les/files/host/sftp_queue.file LIDIS@abc.light.com 2> /path/to/sftp-errorlog
then
        ( echo "Errors during transfer"
          cat /path/to/sftp-errorlog ) >> /red/..../
fi

rm -f /path/to/sftp-errorlog