Bash Continue on Specific Return Code

Bash break and continue

Using loops you can run multiple commands until the condition is satisfied. But sometimes you need to divert the flow of loop or terminate the loop iteration. In such cases, you can use break and continue statements in bash to handle the loop execution. In this guide we will how to use break and continue statements in Bash script.

Bash break and continue

Bash break Statement

The break statement will terminate the current loop and pass the control to the following statement or command. Generally, it is used to exit from a for, while, until or select loops.

Following is the basic syntax for the break statement:

break [n]

Here, the [n] is an optional argument and must greater than or equal to 1. [n] is used to terminate and exit from the n-th enclosing loop. break 1 is similar to break.

Let's see an example for better understanding. In following script, in while loop when the third iteration occur it should terminate:

                  i=0  while [[ $i -lt 5 ]] do   echo "Number: $i"   ((i++))   if [[ $i -eq 3 ]]; then     break   fi done  echo 'That's it!'                
Number: 0 Number: 1 Number: 2 That's it!

Another example of using the break statement inside nested for loops. If the argument [n] is not provided then break terminates the current inner loop and outer loops will not terminate.

                  for i in {1..3}; do   for j in {1..3}; do     if [[ $j -eq 2 ]]; then       break     fi     echo "j: $j"   done   echo "i: $i" done  echo 'That's it!'                
j: 1 i: 1 j: 1 i: 2 j: 1 i: 3 That's it!

To exit from the outer loop, you should use the break 2. Which will break and terminate the second enclosing loop:

                  for i in {1..3}; do   for j in {1..3}; do     if [[ $j -eq 2 ]]; then       break 2     fi     echo "j: $j"   done   echo "i: $i" done  echo 'That's it!'                
j: 1 That's it!

Bash continue Statement

The continue statement will skip the remaining commands inside the body of the enclosing loop for the current iteration. Then it will pass the program control to the next iteration of the loop.

The common syntax of the continue statement is as follows:

continue [n]

Same as break, the [n] is an optional argument and can be greater or equal to 1. When [n] is given, the n-th enclosing loop is resumed. continue 1 is similar to continue.

In the example below, once the current iterated item is equal to 2, the continue statement will cause execution to return to the beginning of the loop and to continue with the next iteration.

                  i=0  while [[ $i -lt 5 ]]; do   ((i++))   if [[ "$i" == '2' ]]; then     continue   fi   echo "Number: $i" done  echo 'That's it!'                
Number: 1 Number: 3 Number: 4 Number: 5 That's it!

In below example, it will show the numbers from 1 through 50 which are divisible by 7. If a number is not divisible by 7, the continue statement skips the echo command and pass control to the next iteration of the loop.

                  for i in {1..50}; do   if [[ $(( $i % 7 )) -ne 0 ]]; then     continue   fi   echo "Divisible by 7: $i" done                
                  Divisible by 7: 7 Divisible by 7: 14 Divisible by 7: 21 Divisible by 7: 28 Divisible by 7: 35 Divisible by 7: 42 Divisible by 7: 49                

Conclusion

In each programming languages loops are basic concepts and useful for automating repetitive tasks. In Bash, the break and continue statement are used to terminate current loop iteration and divert control to specific statement accordingly.

If you have any question or feedback, please leave a comment below.

If our content helps you, please consider buying us a coffee

Thank you for your support.

hyltonfiette.blogspot.com

Source: https://tecnstuff.net/bash-break-and-continue/

Belum ada Komentar untuk "Bash Continue on Specific Return Code"

Posting Komentar

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel