In this section, we will discuss shell loop control in UNIX. So far you have looked at create loops and working with loops to achieve different tasks. Sometimes you require stopping a loop or skipping iterations of the loop.
In this section, we will study following two statements that are used to control shell loops−
All the loops have a limited life and they come out once the condition is false or true depending on the loop.
A loop may continue forever if the essential condition is not met. A loop that execute forever without terminating executes for an infinite number of times. For this reason, such loops are called infinite loops.
Here is a simple instance that uses the while loop to show the numbers zero to nine –
This loop continue forever because a is always greater than or equal to 10and it is never less than 10.
The break statement is used to end the implementation of the entire loop, after finishing the execution of all of the lines of code up to the break statement. It then steps down to the code following the end of the loop.
The following break statement is used to come out of a loop –
The break command can also be used to exit from a nested loop using this format –
Here n specifies the nth enclosing loop to the exit from.
Here is a simple instance which shows that loop terminates as soon as becomes 5 −
Upon execution, you will accept the following result –
Here is a simple instance of nested for loop. This script breaks out of both loops if var1 equals 2 and var2 equals 0 –
Upon effecting, you will obtain the following result. In the inner loop, you have a break command with the argument 2. This indicate that if a condition is met you should break out of outer loop and finally from the inner loop as well.
The continue statement is parallel to the break command, excepting that it causes the current iteration of the loop to exit, rather than the total loop.
This statement is helpful when an error has occurred but you want to try to perform the next iteration of the loop.
Like with the break statement, an integer argument can be given to the continue command to skip commands from nested loops.
Here n specifies the nth enclosing loop to continue from.
The following loop makes use of the continue statement which proceeds from the continue statement and starts processing the next statement –
Upon implementation, you will accept the following result –
Unix/Linux Related Interview Questions
|Perl Scripting Interview Questions||Shell Scripting Interview Questions|
|Python Interview Questions||Core Java Interview Questions|
|Linux Interview Questions||Linux Embedded systems Interview Questions|
|Red Hat Linux System Administration Interview Questions||IBM AIX Interview Questions|
|Sybase Interview Questions||IBM AIX 7 Administration Interview Questions|
|Unix Shell Scripting Interview Questions||Linux Shell Scripting Interview Questions|
Unix/Linux Related Practice Tests
|Perl Scripting Practice Tests||Shell Scripting Practice Tests|
|Python Practice Tests||Core Java Practice Tests|
|Linux Practice Tests||Linux Embedded systems Practice Tests|
|Red Hat Linux System Administration Practice Tests||IBM AIX Practice Tests|
|Sybase Practice Tests||IBM AIX 7 Administration Practice Tests|
|Autosys Practice Tests|
All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd
Wisdomjobs.com is one of the best job search sites in India.