Infinite loop not looping


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Infinite loop not looping
# 1  
Old 03-23-2009
Infinite loop not looping

Hi guys, I'm having a problem getting my infinite loop to loop. It simply reads in the users choice form the menu, executes the corresponding case statement and quits instead of looping back to the main menu again. I have a feeling it might be something with my if then statements within the case statements. Sorry for the long code but here it is. It was done with C shell and everything works great, I just need it to have an infinite loop until user chooses 4 from the menu, in which case the loop ends. Thanks.
Code:
set num = 1
while ($num == 1)
echo "   What would you like to do?"
echo " "
echo "        1 => Install Files"
echo "        2 => Restore Original Files"
echo "        3 => Load File from Backup Folder"
echo "        4 => Exit"
echo " "
set ANSWER=$<
#
# Install Files
# Determine whether a CD or floppy disk was inserted, then proceed to 
# according tasks 
#
switch ($ANSWER)
   case 1:
      if (! -d FILES_BACKUP) then                          
      
         mkdir FILES_BACKUP
        
         if (-f ../cdrom/FILES.tgz) then    
         
            cp -r ../cdrom/FILES.tgz  FILES_BACKUP      
      
         else if (-f ../dev/FILES.tgz) then      
      
            cp -r ../dev/FILES.tgz FILES_BACKUP         
      
         else
      
            echo "====INSERT CD OR FLOPPY DISK===="
          
         endif
              
         cd FILES_BACKUP
         tar xvf FILES.tgz
         mv FILES FILES.1
         rm FILES.tgz
         echo "Installed files successfully"
         echo " "
         echo "   Run as current file [Y or N]?"
         echo " "
   
         set CURRENT=$<
         if ($CURRENT == Y) then            
          
            mv ../FILES FILES.QA   
            cp -r FILES.1 ../FILES
            echo "Current files updated with new file" 
      
         else if ($CURRENT == N) then 
      
            echo "Files have been saved"
         
         else
      
            echo "Bad command"
        
         endif      
      
      
      else if (-d FILES_BACKUP) then
           
         if (-f ../cdrom/FILES.tgz) then
     
            cp -r ../cdrom/FILES.tgz FILES_BACKUP
         
         else if (-f ../dev/FILES.tgz) then
      
            cp -r ../dev/FILES.tgz FILES_BACKUP  
         
         else
         
            echo "====INSERT CD OR FLOPPY DISK===="      
                 
         endif
      
         cd FILES_BACKUP
         tar xvf FILES.tgz
                          
         if (! -d FILES.1) then
         
            mv FILES FILES.1
            rm FILES.tgz
            echo "Installed files from floppy disk successfully"
            echo " "
            echo "   Run as current file [Y or N]?"
            echo " "
   
            set CURRENT=$<
   
            if ($CURRENT == Y) then
            
               mv ../FILES FILES.QA   
               cp -r FILES.1 ../FILES
               echo "Current files updated with new file" 
   
            else if ($CURRENT == N) then 
      
               echo "Files have been saved"
           
            else
         
               echo "Bad command"
            
            endif
 
         else
         
            set i = 1
         
            while (-e "FILES."$i) 
               
               @ i = ($i + 1)
                          
            end
            
            mkdir "FILES."$i
            mv FILES/* "FILES."$i        
            rm -r FILES
            rm FILES.tgz
            echo "Installed files from floppy disk successfully"
            echo " "
            echo "   Run as current file [Y or N]?"
            echo " "
   
            set CURRENT=$<
    
            if ($CURRENT == Y) then
            
               if (-e FILES.QA/) then
            
                  rm -r ../FILES
                  cp -r FILES.$i ../FILES
                  echo "Current files updated with new file"
            
               else
            
                  mkdir FILES.QA
                  mv ../FILES/* FILES.QA   
                  cp -r FILES.$i/* ../FILES
                  echo "Current files updated with new file" 
            
               endif
         
            else if ($CURRENT == N) then
      
               echo "Files have been saved"
            else
         
               echo "Bad command"
         
            endif
         
         endif
    
      endif   
   
   breaksw
#
# Restore Files
# Move contents of "/FILES.QA" back to the original "/FILES" folder
# and delete all temporary folders and files if user chooses.
#
   case 2:
             
      echo " "
      echo "   Would you like to delete all backup files [Y or N]?"
      echo " "
      
      set BACKUP=$<
      
      if ($BACKUP == Y) then
      
         if (-e FILES_BACKUP) then
                  
            if (-e FILES_BACKUP/FILES.QA) then
         
               rm -r FILES
               cp -r FILES_BACKUP/FILES.QA FILES
               rm -r FILES_BACKUP
         
            else
         
               rm -r FILES_BACKUP
            
            endif   
            echo "Backup files removed"
         
         else
         
            echo "Backup files have already been removed"
      
         endif
            
      else if ($BACKUP == N) then 
      
         if (-e FILES_BACKUP) then
         
            if (-e FILES_BACKUP/FILES.QA) then
         
               rm -r FILES     
               cp -r FILES_BACKUP/FILES.QA FILES
               echo "Backup files saved"
         
            endif
                       
         else
      
            echo "There are no backup files to save"
         
         endif   
   
      else
   
         echo "Bad command"
            
      endif
         
      if (-f FILES.tgz) then
   
         rm FILES.tgz
 
      endif
   
   breaksw
   
#
# Load Files from Backup folder
# Prompts user to select file from backup folder to load as the current file
#
   case 3:
       
      if (-e FILES_BACKUP) then
   
         echo " "
         echo "   Which file would you like to load?:"
         echo " "
         ls FILES_BACKUP
         echo " "
   
         set BACKUP=$<
   
         while (! -e FILES_BACKUP/$BACKUP)
      
            echo "That file doesn't exist, Enter filename [9 to exit]: "
            set BACKUP=$<   
    
            if ($BACKUP == 9) then
           
               exit
         
            endif
   
         end            
   
      else
   
         echo "There are no backup files to load"
      
      endif   
   
      rm -r FILES
      cp -r FILES_BACKUP/$BACKUP FILES      
      echo "Files loaded successfully"
   
   breaksw
      
   case 4:           
      
      @ num = ($num + 1)
   
   breaksw
     
   default:
      
      echo "Bad command"
      
   breaksw
   
endsw
end

# 2  
Old 03-23-2009
One thing I've noticed that's helped me in the past with loops and other scripts. Add a lot of echo lines so you know where the code is getting to, and also add in some counters so you know if it fails the first time, second time, etc...
# 3  
Old 03-23-2009
hootdocta5,

Your script works for me.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Infinite loop query

I have a script script.shwhich is scheduled to run at 11 AM everyday. # script.sh Code: ./scb_script.sh & unfortunately scb_script.sh is running today in infinite loop as respective files are not available. My question, when script.sh starts running tomorrow, will the old process be... (1 Reply)
Discussion started by: JSKOBS
1 Replies

2. Shell Programming and Scripting

Infinite looping in script

we have one script which we use to send mail in our environment. If we are giving correct attachment script runs fine but if we give a attachment name which is not present on server then this script go to infinite loop and causing all memory to be used. could any one please suggest me what is wrong... (2 Replies)
Discussion started by: anshu ranjan
2 Replies

3. Shell Programming and Scripting

My for loop decides to become an infinite loop?

Hi, I was debating if I should put this in the dummies or scripts section, I apologize in advance if I chose poorly. Fairly new to Unix and BASH scripting but I thought I made it fairly well given my limited understanding. However, the output indicates that it's looping and I'm ending up with a... (5 Replies)
Discussion started by: gotreef
5 Replies

4. Homework & Coursework Questions

Help with infinite loop problem

1. The problem statement, all variables and given/known data: My problem is an infinite loop when i press any other key other then Y or y in the while loop. what i want it to do is return to the normal script outside of it if pressing N or n or keep asking the same question if its any other... (4 Replies)
Discussion started by: Ren_kun
4 Replies

5. Shell Programming and Scripting

Call a infinite loop

Hi All, I need to run an infinite loop. requirement below: function1 --> creates a file file1 function2 ---> need to call if the file creates i am running these both function via a script --> script.sh i need to run the function1 first and if the file file1 creates then need to run the... (3 Replies)
Discussion started by: satyaranjon
3 Replies

6. UNIX for Advanced & Expert Users

Procmail and infinite loop

I wanted to copy (not forward but copy) all incoming email to another address of mine. It worked, but now I encountered an infinite loop problem: When the second address doesn't like the content and bounces the message back, the bounce message will be sent back and forth. So, what I have in... (1 Reply)
Discussion started by: distill
1 Replies

7. Shell Programming and Scripting

Infinite while loop

what is the difference between while:,while true and while false? (6 Replies)
Discussion started by: proactiveaditya
6 Replies

8. Shell Programming and Scripting

infinite while do loop problem

hi all, this is how my scrip looks like #!/bin/sh bindir='/opt/apps/script/bin' datadir='/opt/apps/script/data' dir='/opt/apps/script' while : ; do ls -1rt /opt/apps/script/data/check.txt*|tail -1 > /dev/null 2>&1 if ;then chmod +rwx $bindir/dummy2.sh ... (8 Replies)
Discussion started by: tententen
8 Replies

9. Programming

Is my code in an Infinite Loop?

Production C code compiled without the dash-g option is running, and seems to be in an infinite loop. Is there a way to tell? Is there a diagnostic tool that will report what objects or what lines of code or even what functions are being executed? Or is my best option to kill it with a dump? ... (5 Replies)
Discussion started by: marcus121
5 Replies

10. Solaris

ls command in infinite Loop

Hi, whenever I am giving a 'ls' command system is going into infinite loop displaying the current home directory. There is no separate shell script/file with ls name anywhere in the system. I am using Solaris 10. Any help / guidance in solving this problem is highly appreciated. ... (3 Replies)
Discussion started by: umakant
3 Replies
Login or Register to Ask a Question