11.5 Breaking a Loop

   

There may be situations when you want to break or discontinue the execution of commands inside the command block of a loop. This is done when a particular condition is met and you don't want any further execution of commands in the command block. You may also need to check an error condition and discontinue execution of the program depending on that error condition.

The shell provides three mechanisms for breaking the normal execution of loops . These are break , continue , and exit .

The break Command

The break command discontinues the execution of loop immediately and transfers control to the command following the done keyword. You can pass a number n as an argument to the break command. In that case, the break command jumps to the command after n th occurrence of the done keyword. Consider script-25 shown here. It asks you to enter a file name . If the file is a regular file, it uses the cat command and displays its contents. If the file is not a regular file, it displays a message and then quits execution after displaying the message " Good bye ." The program uses an infinite loop and will not terminate until you enter a nonregular file name, such as a directory name.

 #!/usr/bin/sh while true do    echo "Enter name of file to be displayed: \c"    read FILE    if [ ! -f $FILE ]    then       echo "This is not a regular file"       break    fi    cat $FILE done echo "Good bye" 

Let us execute and enter a nonregular file such as /etc .

 $  ./script-25  Enter name of file to be displayed:  /etc  This is not a regular file Good bye $ 

The continue Command

The continue command is slightly different from the break command. When encountered , it skips the remaining part of the loop and transfers the control to the start of the loop for the next iteration. The script-26 does the same job as script-25 but with the use of the continue command. In this script, we have changed the test condition, and the loop goes on executing until you enter a file name that is not a regular file. At this point, the loop breaks and the command after the loop is executed.

 #!/usr/bin/sh while true do    echo "Enter name of file to be displayed: \c"    read FILE    if [ -f $FILE ]    then       cat $FILE       continue    fi    echo "This is not a regular file"    break done echo "Good bye" 

The exit Command

The exit command completely terminates the program. It returns an exit code that is optionally provided as its argument in the program. If the exit command doesn't have any arguments, it returns the exit code of the command executed just before it. This command is used when a critical error is encountered, and further execution of the program may cause faulty results. For example, dividing a number by zero is illegal, so you want to check this condition before a command is executed that divides a number by zero. Program script-27 reads a number entered by the user and then divides 100 by this number. It then displays the quotient and the remainder. If you try to divide by zero, the program displays an error message and terminates immediately.

 #!/usr/bin/sh NUM=100 while true do    echo "Enter a divisor for integer 100 : \c"    read DIV    if [ $DIV -eq 0 ]    then       echo "Divide by zero is not permitted"       exit 1    fi    (( QUO = NUM / DIV ))    (( REM = NUM % DIV ))    echo "The quotient is  : $QUO"    echo "The remainder is : $REM" done 

   
Top


HP Certified
HP Certified: HP-UX System Administration
ISBN: 0130183741
EAN: 2147483647
Year: 2000
Pages: 390
Authors: Rafeeq Rehman

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net