Loop Forever Shell Script

Posted on

In this chapter, we will discuss shell loop control in Unix. So far you have looked at creating loops and working with loops to accomplish different tasks.

Loop Forever Shell Script Online

Sometimes you need to stop a loop or skip iterations of the loop.In this chapter, we will learn following two statements that are used to control shell loops−.The break statement.The continue statementThe infinite LoopAll 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 required condition is not met. A loop that executes forever without terminating executes for an infinite number of times. For this reason, such loops are called infinite loops. ExampleHere is a simple example that uses the while loop to display the numbers zero to nine −#!/bin/sha=10until $a -lt 10 doecho $aa=`expr $a + 1`doneThis loop continues forever because a is always greater than or equal to 10 and it is never less than 10. The break StatementThe break statement is used to terminate the execution of the entire loop, after completing 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. SyntaxThe following break statement is used to come out of a loop −breakThe break command can also be used to exit from a nested loop using this format −break nHere n specifies the n th enclosing loop to the exit from.

ForeverLoop forever shell script font

Shell Script Loop Array

ExampleHere is a simple example which shows that loop terminates as soon as a becomes 5 −#!/bin/sha=0while $a -lt 10 doecho $aif $a -eq 5 thenbreakfia=`expr $a + 1`doneUpon execution, you will receive the following result −012345Here is a simple example of nested for loop. This script breaks out of both loops if var1 equals 2 and var2 equals 0 −#!/bin/shfor var1 in 1 2 3dofor var2 in 0 5doif $var1 -eq 2 -a $var2 -eq 0 thenbreak 2elseecho '$var1 $var2'fidonedoneUpon execution, you will receive the following result. In the inner loop, you have a break command with the argument 2. This indicates that if a condition is met you should break out of outer loop and ultimately from the inner loop as well.1 01 5The continue statementThe continue statement is similar to the break command, except that it causes the current iteration of the loop to exit, rather than the entire loop.This statement is useful when an error has occurred but you want to try to execute the next iteration of the loop. SyntaxcontinueLike with the break statement, an integer argument can be given to the continue command to skip commands from nested loops.continue nHere n specifies the n th enclosing loop to continue from.

Powershell Loop Forever

Download win xp sp3 32 bit iso version batman. ExampleThe following loop makes use of the continue statement which returns from the continue statement and starts processing the next statement −#!/bin/shNUMS='1 2 3 4 5 6 7'for NUM in $NUMSdoQ=`expr $NUM% 2`if $Q -eq 0 thenecho 'Number is an even number!!' Continuefiecho 'Found odd number'doneUpon execution, you will receive the following result −Found odd numberNumber is an even number!!Found odd numberNumber is an even number!!Found odd numberNumber is an even number!!Found odd number.