Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

UNIX Shell Loop Control


May 23, 2021 UNIX Getting started


Table of contents


Shell cycle control

So far you've learned to create loops and use loops to accomplish different tasks. Sometimes you need to stop the loop or jump out of the loop iteration.

In this tutorial you'll learn the following statements to control the shell loop:

  • Break statement
  • The continue statement

Unlimited loops

All loops have a limited life cycle. They jump out of the loop when the condition is false or true, depending on the loop.

A loop may execute indefinitely because it does not match the appropriate conditions. A loop that never ends executes is executed countless times. Therefore, this loop is called an infinite loop.

Example

This is a simple example of using a while loop to display numbers 0 through 9:

    #!/bin/sh

    a=10

    while [ $a -ge 10 ]
    do
       echo $a
       a=`expr $a + 1`
    done

This loop will last forever because a is always greater than or equal to 10, and it is never less than 10. So this is a case in point for infinite looping.

Break statement

All break statements are executed after the break statement is executed before the break statement is executed, and the break statement is used to jump out of the entire loop. T hen execute the code behind the loop body. Then run the next code at the end of the loop.

Grammar

The following break statement will be used to jump out of a loop:

    break

Break statements can also use this format to exit nested loops:

    break n

Here n specifies the number of times the closed loop is executed and then exits the loop.

Example

Here is a simple example of how a cycle will terminate as soon as a becomes 5:

    #!/bin/sh

    a=0

    while [ $a -lt 10 ]
    do
       echo $a
       if [ $a -eq 5 ]
       then
      break
       fi
       a=`expr $a + 1`
    done

This results in the following:

    0
    1
    2
    3
    4
    5

Here is an example of a simple nested for loop. If var1 equals var2 and var2 equals 0, the script jumps out of this double loop:

    #!/bin/sh

    for var1 in 1 2 3
    do
       for var2 in 0 5
       do
      if [ $var1 -eq 2 -a $var2 -eq 0 ]
      then
     break 2
      else
     echo "$var1 $var2"
      fi
       done
    done

This results in the following results. I n the inner loop, there is a break command with a parameter of 2. This indicates that you should break the outer and inner loops to meet the conditions.

    1 0
    1 5

The continue statement

The continue statement is similar to the break command, except that the continue statement term ends the current loop and causes the iteration of the current loop to exit rather than the entire loop.

This statement is useful when the program has an error, but you want to execute the next loop.

Grammar

    continue

As with a break statement, an integer argument can be passed to the continue command to skip the command from the nested loop.

    continue n

Here n specifies the number of times the closed loop is executed and then goes to the next loop.

Example

Here's a loop that uses the continue statement, which returns the continue statement and starts processing the next statement:

    #!/bin/sh

    NUMS="1 2 3 4 5 6 7"

    for NUM in $NUMS
    do
       Q=`expr $NUM % 2`
       if [ $Q -eq 0 ]
       then
      echo "Number is an even number!!"
      continue
       fi
       echo "Found odd number"
    done

This results in the following:

    Found odd number
    Number is an even number!!
    Found odd number
    Number is an even number!!
    Found odd number
    Number is an even number!!
    Found odd number