You can control two commands inside the loop: The break command continue command
Break command
The break command is a simple way to jump out of a loop during a process. You can use the break command to jump out of any type of loop, including while loops and until loops
Jump out of a single loop
#!/bin/bash
#breaking out of A For loop
For var1 in 1 2 3 4 5
Do
If [$var 1-eq 3]
Then
Break
Fi
echo "Iteration Number: $var 1"
Done
echo "The For loop in completed"
When Var1=3, jump out of the loop!
[Email protected] ~]#./test17.sh
Iteration Number:1
Iteration Number:2
The For loop in completed
2. Jumping inside the loop
#!/bin/bash
#breaking out of an inner loop
For ((a = 1; a < 4; a++))
Do
echo "out side loop: $a"
for ((b = 1; b <; b++))
Do
If [$b-eq 5]
Then
Break
Fi
echo "in loop: $b"
Done
Done
[Email protected] ~]#./test19.sh
Out Side Loop:1
In Loop:1
In Loop:2
In Loop:3
In Loop:4
Out Side Loop:2
In Loop:1
In Loop:2
In Loop:3
In Loop:4
Out Side Loop:3
In Loop:1
In Loop:2
In Loop:3
In Loop:4
3. Jump out of the outer loop: Break n
n indicates the loop level to jump out, the default n=1, which represents jumping out of the current loop! If n=2, the outer loop will be stopped
#!/bin/bash
#breaking out of an outer loo
for ((a = 1; a < 4; a++))
Do
echo "Out Loop: $a"
for ((b = 1; b <100; b++))
Do
If [$b-GT 4]
Then
Break 2
Fi
echo "Inner Loop: $b"
Done
Done
[Email protected] ~]#./test20.sh
Out Loop:1
Inner loop:1
Inner Loop:2
Inner Loop:3
Inner Loop:4
Continue command
Continue is an early stop loop inside the command, rather than completely terminating the loop Method!
#!/bin/bash
#using the Continue command
For ((var1 = 1; var1 <; var1++))
Do
if [$var 1-gt 3] && [$var 1-lt]
Then
Continue
Fi
echo "Iteration Number: $var 1"
Done
[Email protected] ~]#./test21.sh
Iteration Number:1
Iteration Number:2
Iteration Number:3
Iteration Number:10
Iteration Number:11
Iteration Number:12
When the If-then statement condition (with a value greater than 5 less than 10) is met, the shell executes the continue command, skipping the remaining commands in the loop, but the loop continues! If the if-the statement condition is not met, then the loop returns to normal!
As with the break command, the Continue command can also specify the loop level:
Continue N
#!/bin/bash
#testing an outer loop
for ((a = 1; a <= 3; a++))
Do
echo "Iteration $a"
For ((b = 1; b < 4; b++))
Do
If [$a-gt 2] && [$a-lt 4]
Then
Continue 2
Fi
var3=$[$a * $b]
echo "The result of $a * $b is $var 3"
Done
Done
[Email protected] ~]#./test22.sh
Iteration 1
The result of 1 * 1 is 1
The result of 1 * 2 is 2
The result of 1 * 3 is 3
Iteration 2
The result of 2 * 1 is 2
The result of 2 * 2 is 4
The result of 2 * 3 is 6
Iteration 3
Redirect the output of the loop:
#!/bin/bash
for ((a = 1; a <; a++))
Do
echo "The number is $a"
Done > Test23.txt
echo "The command is finished"
[Email protected] ~]#./test23.sh
The command is finished
[email protected] ~]# cat Test23.txt
The number is 1
The number is 2
The number is 3
The number is 4
The number is 5
The number is 6
The number is 7
The number is 8
The number is 9
This article is from the Linux Operations sharing blog, so be sure to keep this source http://liangey.blog.51cto.com/9097868/1574060
Linux Control loops