How to use “break” and “continue” statements in shell scripts

The break Statement

The break statement allows you to exit the current loop. It is often used in an if statement that is contained within a while loop, with the condition in the while loop always evaluating to true. This is useful if the number of times the loop is executed depends on input from the user and not some predetermined number.

The break statement exits out of the innermost loop in which it is contained.

$ cat break.ksh 
#!/bin/ksh

# Script name: break.ksh

typeset -i  num=0

while true
 do
     print -n "Enter any number (0 to exit): "
   read num junk
        
    if (( num == 0 ))
        then 
    break
        else 
    print "Square of $num is $(( num * num )). \n"
        fi 
done
print "script has ended"
$ ./break.ksh 
Enter any number (0 to exit): 5 
Square of 5 is 25.

Enter any number (0 to exit): -5 
Square of -5 is 25.

Enter any number (0 to exit): 259 
Square of 259 is 67081.

Enter any number (0 to exit): 0 
script has ended

The continue Statement

Use the continue statement within a loop to force the shell to skip the statements in the loop that occur below the continue statement and return to the top of the loop for the next iteration. When you use the continue statement in a for loop, the variable var takes on the value of the next element in the list. When you use the continue statement in a while or an until loop, execution resumes with the test of the control_command at the top of the loop.

Example of Using the continue Statement

The following continue.ksh example script renames files in the current directory whose names contain a capital letter to lowercase letters. First, the script prints the name of the file on which it is currently working. If this file name contains no capital letters, the script executes the continue statement, forcing the shell to go to the beginning of the for loop and get the next file name

If the file name contains a capital letter, the original name is saved in the orig variable. The name is also copied into the new variable; however, the line typeset -l new at the beginning of the script causes all letters stored in the variable to be converted to lowercase during the assignment. When this is done, the script performs a mv command using the orig and new variables as arguments.

The script then prints a message concerning the name change for the file and gets the next file to be worked on. When the for loop is finished, the Done message prints to let the user know the script finished.

$ cat continue.ksh
#!/bin/ksh

# Script name: continue.ksh

typeset -l new # Changes the variable’s contents  
# to lowercase characters

for file in *
do
    print "Working on file $file..."
        
    if [[ $file != *[A-Z]* ]]
    then 
        continue
    fi
        
    orig=$file
    new=$file
    mv $orig $new 
    print "New file name for $orig is $new."
done

print "Done."
$ cd test.dir
$ ls
Als             a               sOrt.dAtA       slAlk 
Data.File       recreate_names  scR1            teXtfile
$ ../continue.ksh 
Working on file Als... 
New file name for Als is als. 
Working on file Data.File... 
New file name for Data.File is data.file. 
Working on file a... 
Working on file recreate_names... 
Working on file sOrt.dAtA... 
New file name for sOrt.dAtA is sort.data. Working on file scR1... 
New file name for scR1 is scr1. Working on file slAlk... 
New file name for slAlk is slalk. Working on file teXtfile... 
New file name for teXtfile is textfile. 
Done.
$ ls 
a               data.file       scr1            sort.data 
als             recreate_names  slalk           textfile
NOTE: The script does not check if a lowercase version of a file name containing capital letters already exists before executing the mv command. Therefore, it is possible to overwrite an existing file. How could this script be improved to avoid that problem?
Related Post