Link to home
Start Free TrialLog in
Avatar of stevesm
stevesm

asked on

Need help with a shell script - FTP related

Here's my script, and here is the problem - the script is supposed to re-submit whatever is still missing (it could be 1 to 3 files). Currently the script is not working properly. In a few occasions when more than 1 edi files were missing, the scripts only managed to re-submit ONE file. What I would like to do is capture all the files (up to 3) when necessary. I need an explanation as to why this does not work and a solution to correct this problem. Don't worry about the path's names or passwords too much they have been edited.

myname=`basename $0`
mydate=`date +%Y%m%d`
#
ERROR_DIR=/tmp
#
if [ $# -eq 0 ]; then
     SEND_HOST=192.168.106.12
     SEND_USER=whatever
     SEND_PASS=secret
     SEND_DEST=/
     DOWNDIR=/outbound
     RETRDIR=/retrieve
     echo "$mydate `date +%T`:**Processing for Primary System $OBJECT "
else
     SEND_HOST=192.168.106.11
     SEND_USER=whatever
     SEND_PASS=secret
     SEND_DEST=/
     DOWNDIR=/outbound
     RETRDIR=/retrieve
     echo "$mydate `date +%T`:**Processing for Alternate System $OBJECT "
fi
#
ARCHIVEDIR=/processed
SYSDIR=/
ETCDIR=$SYSDIR/etc
# USERS=`cat $ETCDIR/Mail.notify`
#
shdirname()
{
expr \
  ${1-.}'/' : '\(/\)[^/]*//*$' \
  \| ${1-.}'/' : '\(.*[^/]\)//*[^/][^/]*//*$' \
  \| .
}
#
# Procedure used to send files
#
ftpfile()
{
#
echo "$mydate `date +%T`:$OBJECT:**Processing - Sending $OBJECT.req "
#
cp $RETRDIR/$OBJECT.edi $DOWNDIR/$OBJECT.req
#
/usr/bin/ftp -vin >$ERROR_DIR/$OBJECT.put.summary.$$ 2> /$ERROR_DIR/$OBJECT.put.ERRORS.$$ <<EOF
open $SEND_HOST
user $SEND_USER $SEND_PASS
binary
cd $SEND_DEST
put $OBJECT.req
bye
EOF
}
#
# Procedure may be used to check errors
#
ftpcheck()
{
WARNINGS=`grep '226 Transfer' /tmp/$OBJECT.put.summary.$$ | wc -l`
if [ "$WARNINGS" -ne 1 ]; then
    echo "$mydate `date +%T`:$OBJECT:**Ftp has encountered error.. Check $OBJECT.put.summary.$$"
fi
}
#
# Housekeeping...
#
housekeep()
{
#
echo "$mydate `date +%T`:$OBJECT:**Removing $OBJECT .."
#
rm $DOWNDIR/$OBJECT.[Rr][Ee][Qq]
#
}
#
# Script stuff starts here....
#
echo "$mydate `date +%T`:**Starts .."
#
cd $RETRDIR
OBJECTS=`ls *.edi 2>/dev/null | awk -F\. '{ print $1 }' | sort -u | awk '{ printf " %s", $1 }'`
#
# anything to do ?
#
echo "$mydate `date +%T`:**Scanning for Files To Re-Send.."

if [ -z "$OBJECTS" ]; then
      echo "$mydate `date +%T`:**No Files To Re-Send Ending..."
      exit 1
fi
#
# walk the directory process available files....
#
for OBJECT in $OBJECTS;
    do
    TARGETS=`ls $OBJECT*`
    cd $DOWNDIR
    if [ ! -z "$TARGETS" ]; then
       for TARGET in $TARGETS;
           do
             echo "$mydate `date +%T`:$OBJECT:**Processing File:$TARGET ..."
             if [ "$TARGET" = "$OBJECT.ack" -o "$TARGET" = "$OBJECT.ACK" ]; then
                  echo "$mydate `date +%T`:$OBJECT:**Creating Retrieval Entry for File:$OBJECT.ACK ..."
                  touch $RETRDIR/$OBJECT.ACK
                  mv $TARGET $ARCHIVEDIR/$TARGET.$$
             else
                  ftpfile
                  ftpcheck
                  housekeep
             fi
           done
    fi
    done
#
ASKER CERTIFIED SOLUTION
Avatar of bira
bira

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of stevesm
stevesm

ASKER

I need to test this and let you know.
Cheers
Avatar of stevesm

ASKER

My apologies for taking so long to get back, no excuses. I changed the variable as you mentioned and it worked like a charm.


Thanks for your help
Steve