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:
- Break command
- 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
You should read it
- The while loop in Shell
- Loop in Unix / Linux
- For loop in Shell
- Select loop in Shell
- Network communication utilities in Unix / Linux
- Navigation IO in Unix / Linux
- Basic file system in Unix / Linux
- System log in Unix / Linux
- Until loop in Shell
- What is Unix / Linux?
- Basic utilities: print, email in Unix
- Signal and Trap in Unix / Linux
Maybe you are interested
SpaceX's Crew Dragon spacecraft successfully assembled with the ISS station, completely automated Will NASA partner with Tesla to create the most unprecedented lunar exploration vehicle? SpaceX is about to blow up a tens of millions of dollars worth of boosters on the Atlantic WiMax gigabit speed creates 4th generation mobile network The Windows era will continue until 2023 NASA announces a stunning series of GIF images that capture the historic moments of the aerospace industry