Unix/Linux Go Back    


UNIX for Beginners Questions & Answers 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?

UNIX for Beginners Questions & Answers


Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 3 Weeks Ago   -   Original Discussion by BG_JrAdmin
BG_JrAdmin's Unix or Linux Image
BG_JrAdmin BG_JrAdmin is offline
Registered User
 
Join Date: Feb 2005
Last Activity: 22 December 2017, 7:40 AM EST
Posts: 181
Thanks: 1
Thanked 0 Times in 0 Posts
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

Sponsored Links
    #2  
Old Unix and Linux 3 Weeks Ago   -   Original Discussion by BG_JrAdmin
RudiC's Unix or Linux Image
RudiC RudiC is online now Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 17 January 2018, 2:48 PM EST
Location: Aachen, Germany
Posts: 11,940
Thanks: 352
Thanked 3,678 Times in 3,376 Posts
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.
Sponsored Links
    #3  
Old Unix and Linux 3 Weeks Ago   -   Original Discussion by BG_JrAdmin
MadeInGermany's Unix or Linux Image
MadeInGermany MadeInGermany is offline Forum Staff  
Moderator
 
Join Date: May 2012
Last Activity: 17 January 2018, 2:10 PM EST
Location: Simplicity
Posts: 3,929
Thanks: 333
Thanked 1,314 Times in 1,184 Posts
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 Unix and Linux 3 Weeks Ago   -   Original Discussion by BG_JrAdmin
RudiC's Unix or Linux Image
RudiC RudiC is online now Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 17 January 2018, 2:48 PM EST
Location: Aachen, Germany
Posts: 11,940
Thanks: 352
Thanked 3,678 Times in 3,376 Posts
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 (3 Weeks Ago)
Sponsored Links
    #5  
Old Unix and Linux 3 Weeks Ago   -   Original Discussion by BG_JrAdmin
MadeInGermany's Unix or Linux Image
MadeInGermany MadeInGermany is offline Forum Staff  
Moderator
 
Join Date: May 2012
Last Activity: 17 January 2018, 2:10 PM EST
Location: Simplicity
Posts: 3,929
Thanks: 333
Thanked 1,314 Times in 1,184 Posts
Yes that should work and is most efficient!
Sponsored Links
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Help with loops? jose2802 UNIX for Advanced & Expert Users 3 10-20-2014 12:54 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
Loops Shazin Shell Programming and Scripting 4 08-04-2009 10:09 AM
While Loops vdc UNIX for Dummies Questions & Answers 4 05-10-2005 09:55 AM



All times are GMT -4. The time now is 04:13 PM.