Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

bourne shell error handling for copy files

Status
Not open for further replies.

rozzay

Programmer
Jan 3, 2002
142
US
Hi I have a bourne shell script that copies a file to another server or to a shared directory is there a way to do some kind of error handling if the file is unsuccessfully when copying over? Any help or links would be great.

#!/bin/sh
EDIOLD_DIR=/usr/local/cyclone/data/14001695568GT/ediold GIS_DIR=/usr/local/GIS4/AAFES/CYCLONE_D
LOG_DIR=/usr/local/cyclone/logs INBOUNDFILE=$1
FILENAME=`basename $INBOUNDFILE`
DATETIME=`date +%m/%d/%y" "%H:%M:%S` ID=14001695568GT
SHORTFN=`echo $FILENAME |sed -e 's/\(.*\)'$ID'\(.*\)/\1\2/'`
echo $SHORTFN

#cp $INBOUNDFILE $EDIOLD_DIR/tmp/$SHORTFN
cp $INBOUNDFILE $GIS_DIR/$SHORTFN
chmod 777 $GIS_DIR/$SHORTFN

# Move Inbound EDI file to EDIOLD directory
mv $INBOUNDFILE $EDIOLD_DIR/$FILENAME
 
After every command that you want to 'error-handle', put a test clause:

example:

cp $INBOUNDFILE $GIS_DIR/$SHORTFN
errcode=$?
if test $errcode -ne 0
then
echo "An error occurred while running command "cp $INBOUNDFILE $GIS_DIR/$SHORTFN"
exit $errcode
fi

Of course it's for you to decide what action to take, here I chose to exit...

Also you can trap error output in a logfile:

rm -f /tmp/GIS.err
cp $INBOUNDFILE $GIS_DIR/$SHORTFN 2>/tmp/GIS.err
errcode=$?
if test $errcode -ne 0
then
echo "An error occurred while running command "cp $INBOUNDFILE $GIS_DIR/$SHORTFN"
cat /tmp/GIS.err
exit $errcode
fi

You take it from here?

HTH,

p5wizard
 
We use the sum command to help validate a successful file copy for large (or important) files. This produces a checksum value, so in the script generate a checksum before the file is copied, then another checksum on the resultant file and compare the checksums. (if the same then OK, if not then repeat the process or send a mail message or error out)

Another trick we use to 'protect' the destination file from being accessed too early (ie before the copy has finished) is to create a 'lock-file', do the copy, then delete the 'lock-file'.

I hope that helps.

Mike
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top