Control loop in Unix / Linux

The previous chapter has been shown how to create loops and work with them to perform various tasks. Sometimes you need to stop a loop or continue their repetitive process.

The previous chapter has been shown how to create loops and work with them to perform various tasks. Sometimes you need to stop a loop or continue their repetitive process.

In this chapter, you will learn two commands used in Shell loop control:

  1. Break command
  2. Continue statement

Infinite loop in Unix / Linux

All loops have a limited lifecycle and they end when the condition is true or false depending on the loop.

A loop can continue to proceed permanently because it does not match the required conditions. An infinite loop is a loop that runs for countless times.

For example

Here is a simple example of using a while loop to display numbers from 0 to 9.

 #! / bin / sh a = 10 while [ $ a - ge 10 ] by echo $ aa = `expr $ a + 1` done 

This loop is an infinite loop because a is always greater than or equal to 10 and it will never be less than 10.

Break command in Unix / Linux

The break command is used to end the running process of a full loop, after completion of running all the lines in the code to this break command. It then steps out of the code after finishing the loop.

Syntax

The following break statement is used to exit a loop.

 break 

The break command can be used to exit a loop using the following format:

 break n 

Here n identifies the nth loop to be surrounded to exit the word:

For example

The following example only the following loop will end immediately after the condition becomes 5:

 #! / bin / sh a = 0 while [ $ a - lt 10 ] by echo $ a if [ $ a - eq 5 ] then break fi a = `expr $ a + 1` done 

It will display the following result:

 0 1 2 3 4 5 

Here is a simple example of loop nesting. Script exits both loops if var1 equals 2 and var2 equals 0.

 #! / bin / sh for var1 in 1 2 3 due to var2 in 0 5 because if [ $ var1 - eq 2 - a $ var2 - eq 0 ] then break 2 else echo "$ var1 $ var2" fi done done 

It will produce the following result. In the inner loop you have a break statement with argument 2. It indicates that if the condition is satisfied you should exit the outer loop.

 1 0 1 5 

The continue command in Unix / Linux

This continue statement is similar to the break statement, except that it causes the current iteration of the loop to exit, not the entire loop.

This command is used when an error has occurred and you want to try to run the next iteration of the loop.

Syntax:

 tiếp tục 

As with the break statement, an integer argument can be provided to the continue statement to jump through this command from the nested loop.

 continue n 

Here n identifies the n-loop loop to continue from there.

For example

The following loop uses the continue command that comes back from the continue command and starts the next command.

 #! / 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 "Found odd number" done 

It will produce the following result:

 Tìm số thứ tự số không là một số thứ tự !! Tìm số thứ tự số không là một số thứ tự !! Tìm số thứ tự số không là một số thứ tự !! Found odd number 

According to Tutorialspoint

Previous article: Loop in Unix / Linux

Next article: Shell replacement

4 ★ | 1 Vote