Break vs Continue


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Break vs Continue
# 1  
Old 06-05-2012
Break vs Continue

Okay so I am having trouble understand what the computer will do with a code like this

Code:
 if [ file exists ] ; then
                        echo
                        echo "Found the file"
                        blah blah blah
                        for i in `blah blah blah` ; do
                                echo $i
                        done >thing ; sed "/$blah/d" thing >thing2
                        break
                else
                        continue
                fi
        done
                        blah blah blah
                        blah blah blah

I am just trying to figure out what would happen when you hit the break and if the file was not found what would happen if you hit continue. Does the done command effect what would happen next if you hit either break or continue?

Last edited by Scrutinizer; 06-05-2012 at 01:19 PM..
# 2  
Old 06-05-2012
Hi, it helps to properly indent the code:
Code:
  if [ file exists ] ; then
    echo
    echo "Found the file"
    blah blah blah
    for i in `blah blah blah` ; do
      echo $i
    done >thing
    sed "/$blah/d" thing >thing2
    break
  else
     continue
  fi
done

Then it becomes apparent that the last "done" belongs to a missing loop statement, probably one line above the if statement. The "break" statement means "end the loop now", the "continue" statement means stop processing further statements in the loop and continue with the next iteration in the loop.
# 3  
Old 06-05-2012
I agree with Scrutinizer but would add that the continue is un-needed in that situation.

removing the else and continue lines would also end that iteration and "continue" to the next iteration since it is already at the end of the loop code (immediately above done).

That is just extra overhead and time that will never serve a valid purpose.

If there was more code after that if statement then the continue would be valid, in this case there is not.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Case statement - continue

I have a case statement. IS "continue" working in case? for file in ls dir/* case $file in a) do something continue ;; b) do something continue ;; esac It is a Bourne shell (13 Replies)
Discussion started by: digioleg54
13 Replies

2. Shell Programming and Scripting

If else continue to check value until it it is right

i have script which get Input via READ value and compare it from file. when found do some stuff...if not found again ask for Input until you dont enter Right value. #!/bin/ksh echo "SID must be in oratab file" echo "Enter ORACLE_SID of Database:\c " read ORACLE_SID x=`cat /etc/oratab|... (3 Replies)
Discussion started by: tapia
3 Replies

3. Shell Programming and Scripting

Continue problem

Hey all. First-time poster, long-time reader I'm on Mac, and I've written a long script to open up a maximum of 20 Terminal windows and run a subscript with a different input in each of them. When each of these sub-scripts finishes, it changes the value of a variable ("$windows") by -1, which... (4 Replies)
Discussion started by: Diabadass
4 Replies

4. Shell Programming and Scripting

BASH: Break line, read, break again, read again...

...when the lines use both a colon and commas to separate the parts you want read as information. The first version of this script used cut and other non-Bash-builtins, frequently, which made it nice and zippy with little more than average processor load in GNOME Terminal but, predictably, slow... (2 Replies)
Discussion started by: SilversleevesX
2 Replies

5. Shell Programming and Scripting

Restart and then continue script

How can I get a script to complete a update, varifiy completion, resboot, and continue with script? Is it possbile to get script to add itself to the "startup application" list #!/bin/bash clear sudo apt-get update #Verify/test the update completed #Reboot #Start/comtinue... (9 Replies)
Discussion started by: wolfgangcs
9 Replies

6. Shell Programming and Scripting

Are you sure you want to continue connecting (yes/no) need a way to pass in the value yes without

Are you sure you want to continue connecting (yes/no) need a way to pass in the value yes without use the except command. I am creating a script to send down files to an application servers every time it reboots as it picks up the newest image. I do not want to manual connect to each server... (1 Reply)
Discussion started by: 3junior
1 Replies

7. Shell Programming and Scripting

awk continue

I want to print entire row of file awk '{print $0}' inputfile but sometime before every row have space characters. Example: " HVLR is not in service on AP 54" How can i print entire row without space characters ? thanks (3 Replies)
Discussion started by: anhtt
3 Replies

8. Shell Programming and Scripting

continue example

The following code for search a pattern in file name (or entire file name) and look at its size, code is derived from an ebook about scripting. It is working in HP Unix but I am unable to run in Linux (Ubuntu) Please advise me what is wrong for Linux? And besides , how can I get rid of errors in... (7 Replies)
Discussion started by: xramm
7 Replies

9. Shell Programming and Scripting

Will the continue function work ????

I have written a script which does a following functions:- 1) Check a area if it is mounted or not 2) If the area is not mounted it will prompt the user to mount the are. 3) Once the area is mounted and the option is given as Y or y the script continues... My question is will the below... (2 Replies)
Discussion started by: kamlesh_p
2 Replies

10. UNIX for Advanced & Expert Users

continue the suspended jobs

Guys, Any idea how to continue suspended job in background ? ihave tried to use the bg% command <root> but it doesnt work. unix> jobs +suspended du > usage -suspended (sleep 60; date) unix> bg %2 (sleep 60; date) But my suspended work doesnt seems to continue run in background.. Any... (6 Replies)
Discussion started by: killerserv
6 Replies
Login or Register to Ask a Question