Home Man
Search
Today's Posts
Register

If you're not sure where to post a Unix or Linux question, post it here. All unix and Linux beginners welcome in this forum!

Is it Possible to combine these two loops into one?

Login to Reply

 
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

The Following User Says Thank You to RudiC For This Useful Post:
MadeInGermany (12-22-2017)
# 5  
Old 12-22-2017
Yes that should work and is most efficient!
Login to Reply

« Previous Thread | Next Thread »
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Need help with for loops LilyClaro UNIX for Dummies Questions & Answers 0 02-17-2016 08:16 AM
Too many for loops maverick72 Shell Programming and Scripting 2 05-27-2011 07:36 AM
Two Loops cinderella Shell Programming and Scripting 8 01-20-2011 03:19 PM
Hmm while do loops. EwanD Shell Programming and Scripting 2 12-07-2009 08:31 AM
loops with tr juliette salexa UNIX for Dummies Questions & Answers 2 08-24-2009 12:57 PM
sed used in loops riayi Shell Programming and Scripting 1 10-01-2008 02:59 AM
For Loops msb65 UNIX for Dummies Questions & Answers 3 08-06-2008 05:00 PM
Help with the 2 for loops vadharah Shell Programming and Scripting 1 03-04-2008 03:26 PM
Help with While Loops MaestroRage UNIX for Dummies Questions & Answers 1 02-13-2008 01:04 AM
While loops and awk Loriel Shell Programming and Scripting 1 10-26-2005 12:26 PM


All times are GMT -4. The time now is 01:50 AM.

Unix & Linux Forums Content Copyright1993-2018. All Rights Reserved.
UNIX.COM Login
Username:
Password:  
Show Password