Is it Possible to combine these two loops into one?

 
Thread Tools Search this Thread
# 1  
Old 12-22-2017
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?



Code:
# 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

# 2  
Old 12-22-2017
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.
# 3  
Old 12-22-2017
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:

Code:
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.

# 4  
Old 12-22-2017
Wouldn't this (untested, admittedly) replace ALL of your loops?

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

This User Gave Thanks to RudiC For This Post:
MadeInGermany (12-22-2017)
# 5  
Old 12-22-2017
Yes that should work and is most efficient!

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

More UNIX and Linux Forum Topics You Might Find Helpful
Help with loops? jose2802 UNIX for Advanced & Expert Users 3 10-20-2014 12:54 PM
If and Loops Pcarson Homework & Coursework Questions 1 12-08-2013 03:53 AM
Loops pdathu Shell Programming and Scripting 3 09-26-2011 11:38 PM
Two Loops cinderella Shell Programming and Scripting 8 01-20-2011 04:19 PM
loops yash1978 Shell Programming and Scripting 4 01-05-2011 06:37 AM
Help with loops dsravan Shell Programming and Scripting 0 06-02-2010 09:00 PM
Help with for loops Oshie74 UNIX for Dummies Questions & Answers 5 03-14-2010 09:45 PM
loops with tr juliette salexa UNIX for Dummies Questions & Answers 2 08-24-2009 01:57 PM
For Loops msb65 UNIX for Dummies Questions & Answers 3 08-06-2008 06:00 PM
while loops nymus7 Shell Programming and Scripting 4 03-31-2006 12:59 PM
two loops big123456 UNIX for Dummies Questions & Answers 2 10-11-2005 04:08 AM
While Loops vdc UNIX for Dummies Questions & Answers 4 05-10-2005 09:55 AM
Loops within loops bthomas Shell Programming and Scripting 8 04-14-2005 02:09 PM
Loops mariner UNIX for Advanced & Expert Users 2 07-01-2004 08:50 AM
While loops bookoo UNIX for Dummies Questions & Answers 5 08-23-2002 12:01 PM