Is it Possible to combine these two loops into one?

 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Is it Possible to combine these two loops into one?
# 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:
# 5  
Old 12-22-2017
Yes that should work and is most efficient!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Need help with for loops

Why wont my for statements work? Im trying to get this script to swich to a user an if you put in a start/stop/or restart paramater to do just that for each user. I commented out the actual start/stop actions to test it just by using echos and not do anything hasty in the environment but it... (0 Replies)
Discussion started by: LilyClaro
0 Replies

2. UNIX for Advanced & Expert Users

Help with loops?

I'm trying to understand better the while and until loops, can someone help me with this example? #!/bin/bash # Listing the planets. for planet in Mercury Venus Earth Mars Jupiter Saturn Uranus Neptune Pluto do echo $planet # Each planet on a separate line. done echo; echo for... (3 Replies)
Discussion started by: jose2802
3 Replies

3. Shell Programming and Scripting

Loops

Hi All, I am very new to Shell scripting. I read basic scripting manual. But i didn't understand the code. Please tell the meaning of the below code: while getopts "F:f:R:r:C:c:" opt 2>/dev/null do case ${opt} in F|f) FREQUENCY_MODE=$OPTARG;; ... (3 Replies)
Discussion started by: pdathu
3 Replies

4. Shell Programming and Scripting

loops

Hi All I have some directories on our server which are containing .csv files. i need to print value of cell "B2" from those csv files. Please advise. I have tried head command as example: head -2 */Book_Collection_Report_1_-_Collection_Requests_trials.csv | sed -n "3p" | awk -F","... (4 Replies)
Discussion started by: yash1978
4 Replies

5. UNIX for Dummies Questions & Answers

loops with tr

Hello, I'm not sure if this is more appropriate for the 'unix for dummies' or the 'unix for experts' forum because I'm new to this forum and this is the second topic I've discussed, but if you could let me know which one was more appropriate for something like this, please do! So in tr (an... (2 Replies)
Discussion started by: juliette salexa
2 Replies

6. Shell Programming and Scripting

Loops

Hi All, I want to execute a script the number of times a user enters. Please can you advise on hor can I do the same. Many Thanks, Shazin (4 Replies)
Discussion started by: Shazin
4 Replies

7. UNIX for Dummies Questions & Answers

For Loops

Hi everyone, I have a question regarding for loops. I am writing a BASH shell script that will contain multiple loops. These loops all involve looping with a count: for (( a=0; a <=10; a++ )); do echo $a done If none of my multiple loops relate to each other throughout the script,... (3 Replies)
Discussion started by: msb65
3 Replies

8. Shell Programming and Scripting

while loops

Hi I've a file like so: Now, I want to read my file and take ex. the Media ID and the Type for each groups of Media (Media1,Media2,...,Media(n): cat /tmp/file|\ while read FILE do while $(FILE|cut -d: -f1)=Media$i do #here will be some test, ex: #if Media ID < 23 ... (4 Replies)
Discussion started by: nymus7
4 Replies

9. UNIX for Dummies Questions & Answers

two loops

Hi, how can I use "for" to have two loops : this is my script : for i in (A B C) do for j in (a b c) do echo $i$j done done #End I want to print out Aa Ab Ac .... But I have error message : syntax error at line 1 : `(' unexpected Many thanks before. How should I use "for" ?? (2 Replies)
Discussion started by: big123456
2 Replies

10. UNIX for Dummies Questions & Answers

While Loops

I'm trying to create a loop that will prompt the user for 15 values, not forcing them to enter all 15. If the user enters through one or more of the prompts the null value needs to be converted to 0, otherwise set the parameter = to the value entered: ex. Please enter file no #1: 17920 ... (4 Replies)
Discussion started by: vdc
4 Replies
Login or Register to Ask a Question