while loop inside a for loop | Unix Linux Forums | UNIX for Dummies Questions & Answers

  Go Back    


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

while loop inside a for loop

UNIX for Dummies Questions & Answers


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 10-10-2012
rgomons rgomons is offline
Registered User
 
Join Date: Oct 2012
Last Activity: 10 October 2012, 1:12 PM EDT
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Question while loop inside a for loop

Hi, I am a newbie and would like to create a shell script that will move one file at a time from one path to another path in the same server. However, the next file should wait until the first file gets deleted by an application.

I tried creating the script but right after the first file has been moved the script ends. It did not move the 2nd file.

Here is the script :


Code:
#!/bin/ksh
echo "// *  CHECKING if with file from HOCDT01            "
cd $sourceftpdir
for loopfile in `ls ASGTEST* Wings_Test*`
do
 mv -f $loopfile $targetdir/.
       if [ $? -eq 0 ]
           then
             echo "// * "
             echo "// *     $loopfile MOVED to $targetdir  " `date`
           else
             ret=99
             echo "// *      $loopfile Files not moved " `date`
             exit $ret
       fi
######################################################################
# Check file                                                         #
######################################################################
echo "This is the filename $loopfile"
loop_counter=1
while  [  "$loop_counter" -lt 6 ]
do
   cd $targetdir
   ls $loopfile 
   ret=$?
  if [ $ret -eq 0 ]
  then
    echo " NOT READY FOR NEXT STEP - Loop Counter : $loop_counter"
    if [  "$loop_counter" -eq 5 ]
    then
     ret=20
     echo " Mode Checking exceeded 30 minutes"
     exit $ret
    else
     sleep 300
    fi
  else
         echo "//***************************************"
         echo "//* File taken by application"
         loop_counter=6
  fi
  loop_counter=`expr $loop_counter + 1`
done
done


Help needed. Thanks in advance.

Last edited by Corona688; 10-10-2012 at 02:15 PM..
Sponsored Links
    #2  
Old 10-10-2012
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 27 November 2014, 5:33 PM EST
Location: Saskatchewan
Posts: 19,943
Thanks: 848
Thanked 3,420 Times in 3,205 Posts
That is a useless use of ls and useless use of backticks.

You don't have to use ls to check for the existence of a file, that's what if [ -f "$FILENAME" ] is for. -d checks for a directory.


Code:
# Bad things will happen if these dirs don't exist.
# Test first!
[ -d "$sourceftpdir" ] || exit 1
[ -d "$targetdir" ] || exit 1

ls "$sourceftpdir" | egrep "^(ASGTEST|Wings_Test)" | while read FILE
do
        [ -f "$sourceftpdir/$FILE" ] || continue # Ignore things that aren't files
        if ! mv "$sourceftpdir/$FILE" "$targetdir"
        then
                echo "couldn't move $FILE"
                exit 1
        fi

        S=0
        while [ -f "$targetdir/$FILE" ] && [ "$S" -lt 1800 ]
        do
                sleep 10
                let S=S+10
        done

        if [ -f "$targetdir/$FILE" ]
        then
                echo "File $FILE still exists after 30 minutes"
                exit 1
        fi

        echo "File moved from $inputdir to $targetdir and processed"
done

Sponsored Links
Closed 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
For Loop inside For loop nnani Shell Programming and Scripting 6 04-22-2012 04:12 PM
BASH loop inside a loop question rethink Shell Programming and Scripting 4 09-15-2010 08:58 AM
Is it possible to have a for loop inside another for loop? scottzx7rr Shell Programming and Scripting 4 02-17-2010 05:23 PM
Using variables created sequentially in a loop while still inside of the loop [bash] DeCoTwc Shell Programming and Scripting 2 06-23-2009 05:59 PM
while loop inside while loop panknil Shell Programming and Scripting 0 01-07-2008 12:49 PM



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