Is it Possible to combine these two loops into one?

Hi there! I created a script to upload some files that are in two separate directories at the source, and need to go to two separate directories at the destination, so I made two loops. I was hoping for some suggestions how I could combine this into one loop?

# START of PROGRAM
#
#     Get the "Delete / Removal" files and Upload them.
#
for DELFILES in `ls $SOURCEDELDIR`
   do
      echo $TODAY >> $LOGFILE.$TODAY
          $SFTPCMD -oIdentityFile=$SFTPKEY $SFTPUSER@$SFTPURL >> $LOGFILE.$TODAY <<ENDSCRIPT
            mput $SOURCEDELDIR/$DELFILES $SFTPDELDIR
          exit
ENDSCRIPT
   done
#
#    Get the "Update / Extraction" files and Upload them.
#
for UPTFILES in `ls $SOURCEUPTDIR`
   do
      echo $TODAY >> $LOGFILE.$TODAY
           $SFTPCMD -oIdentityFile=$SFTPKEY $SFTPUSER@$SFTPURL >> $LOGFILE.$TODAY <<ENDSCRIPT
             mput $SOURCEUPTDIR/$UPTFILES $SFTPUPTDIR
           exit
ENDSCRIPT
   done

What be the similarities / commonalities of the two tasks, what aspects do they share? Should there be enough of these, it might make sense to consider a merge. But then, the logics needed inside the loop to tell each case from the other might by far outweigh the savings.
One approach could be to collect the files into a compound mput ftp command (in two loops, of course) and run that in one single ftp invocation and login.
BTW, none of the variables used in your above code snippet is defined and thus will lead to failure when running the code.

You have two input variables that change
SOURCEDELDIR SFTPDELDIR, and then
SOURCEUPTDIR SFTPUPTDIR.

That means a simple outer loop does not work because it ihas only *one* variable.
(Or you find a method to construct the variables from a common variable, say DEL and UPT.)

One approach is to put the common things into a function,
and pass the input variables:

upload(){
  upload_sourcedir=$1
  upload_destdir=$2

  for upload_sourcefile in `ls $upload_sourcdir`
  do
    echo $TODAY
    $SFTPCMD -oIdentityFile=$SFTPKEY $SFTPUSER@$SFTPURL <<ENDSCRIPT
      mput $upload_sourcedir/$upload_sourcefile $upload_destdir
      exit
ENDSCRIPT
  done
}

# open the logfile in append mode and assign descriptor 3
exec 3>>$LOGFILE.$TODAY

upload $SOURCEDELDIR $SFTPDELDIR >&3
upload $SOURCEUPTDIR $SFTPUPTDIR >&3

# Open files are closed when the script exits.

Wouldn't this (untested, admittedly) replace ALL of your loops?

echo $TODAY >> $LOGFILE.$TODAY
$SFTPCMD -oIdentityFile=$SFTPKEY $SFTPUSER@$SFTPURL >> $LOGFILE.$TODAY <<-ENDSCRIPT
     mput $SOURCEDELDIR/* $SFTPDELDIR
     mput $SOURCEUPTDIR/* $SFTPUPTDIR
     exit
     ENDSCRIPT
1 Like

Yes that should work and is most efficient!