Break continue exit return difference in Linux loop structure

Source: Internet
Author: User

1 overview

In the script loop, you need to use break,continue,exit. To control the loop. Use return to get the return value of the function. This article will introduce the use of these four commands

Break [n]: Early end of the nth layer cycle, the inner layer is the 1th layer

Continue [n]: End of the nth layer of the current cycle, and directly into the next round of judgment; the inner layer is the 1th floor.

Exit command to terminate the script

2 Break Continue exit comparison

2.1 Break and Continue test

Assuming a loop body, a total of 3 layers of loops are nested, the total loop body n is 3,break and continue the number of layers to exit is n

To facilitate test comparisons, the continue and break and exit are written in the innermost loop body, which is within 1 of the loop body.

The outermost loop is 3

The second layer loop is 2

The innermost loop is 1

Test method: One test, first comment out the other two

Step one: Test break, the continue and exit are commented, in turn, increase the value of n in Break N, from 1 to n,n final greater than 3

Step two: Test continue, then break, and exit are commented, in turn, increase the value of n in continue N, from 1 to N,n eventually greater than 3

Step three: Compare the results of steps one and two

The test results are as follows

Continue 1, end this sub-cycle condition, exit the loop to the same layer, that is, 1 layers to continue the next condition of the loop

Break 1 ends the loop of this layer, exits to the previous layer (3-1 layers), that is, the 2nd loop, and resumes the loop of the next condition of the 2nd layer loop

Continue 2, this condition is equivalent to break 1

Break 2 ends a two-tier loop that exits the previous layer of the previous loop (layer 3-2), the 1th loop, and resumes the loop of the next condition of the 1th layer loop

Continue 3, this condition is equivalent to break 2

Break 3, end the three-layer loop, exit 3 loop bodies, and continue to execute the script below the loop body.

Note that when the number of exited loop bodies n is greater than 3, the results obtained at this time

Continue N obtained the same result as continue 3;

Break N gets the same result as break 3;

Summary: N is the outermost layer of all the loop bodies, when the number of layers of exit loop n is 1<n<n,continue n equivalent to break n-1,

When N=1 or n=n;continue and break do not perform the same effect

When N>n, there is actually no actual execution effect, because not so many layers of the loop body can exit

2.2 Exit Test

Exit X, once executed to exit, exits the entire script, where X is not the exit layer, only the value of $ after the exit function.

As set

Exit 6

When the loop executes to this statement, exit all scripts directly, and the value of $? is 6

2.3 Scripts

#!/bin/bash##****************************************************************************** #Author:                Sunny#Date:                  2017-09-01#filename:              tbreak_con_exit.sh#version:               1.0#Your change  info:      #Description:           For testing break continue and exit#copyright (C):          2017  all rihts reserved#*********************************************** for  ((m=1;m<3;m++)) do    for  ((j=1;j<5;j++)) ;d O        for  ((i=1;i<5;i++ ))         do           if [  $i  -eq 3 ]          then             break 4         #continue  3          #exit  6        fi           echo  "the m cycle is  $m  the  out cycle is  $j,and inner cycle is  $i "         done    donedoneif [  $j  -eq 1 ];thenecho  " now it will exit, the m:j:i cycle is  $m:  $j  :  $i " elseecho  "Now it will exit, the m:j:i cycle is $[  $m -1 ]: $[  $j -1 ] :  $[  $i -1 ] "fi


3 return

Return this is a built-in command that can only be identified in a function or in the Execute source script

Return must be followed by a number

3.1 Return n in the test function

When the function encounters a return n, the default function ends, and the value of n is the return value of the entire function execution result, if the return value is 0, indicating that the function is running correctly, and if 1-255 indicates that the result of the execution is wrong.

Test method: By adjusting the n value of return n, the n range is [0,255]

The results are as follows:

When N=0, the call to function Get_version executes the code {echo "\$?=$?; Rrc1=$? ";}

When n! = 0, the calling function Get_version executes the code {echo "\$?=$?; Wrc2=$? ";}

which

RC1 will assign a value of 0, because at this time the $? is the return value that is executed to get the version information os_version, the command executes the result is correct, so returns 0 value

RC2 will not be assigned, because RC2 already has a return n, and the function executes to return n has ended, so RC2 is not assigned

The script is as follows:

#!/bin/bash##****************************************************************************** #Author:                Sunny#Date:                  2017-09-01#filename:              Treturn.sh#version:               1.0#Your change info:       #Description:           for#copyright (C):          2017  all rihts reserved#******* Get_version ()  {     os_version= ' cat /etc/system-release | grep -o  " [0-9]" | cut -d   " "  -f2 ' && { RC1=$?; return 2; Rc2=$?;  }    }   get_version && { echo  "\$?=$?; Rrc1=$? ";  } | |  { echo  "\$?=$?; Wrc2=$? ";  }echo version= $os _versionecho rc1= $RC 1echo rc2= $RC 2

3.2 Testing the return n of the source script

When executing the source script, if a return n is encountered indicating the end of the entire script execution, and the N value as the return value of the end of the script

Test method: By commenting out the os_version to see if there is no return in the script after the statement will be executed

The results are as follows:

When there is return N, the command after return n is not executed, indicating that the script has been exited

When there is no return n, the command after return n is still executed.

#!/bin/bash##****************************************************************************** #Author:                Sunny#Date:                  2017-09-01#filename:              trc.sh#version:               1.0#Your change info:       #Description:          for   test the functioon of return used in script#copyright (C):          2017  all rihts reserved#*********************** echo rc1= $RC 1echo rc2= $RC 2echo \$?=$?os_ Version= ' Cat /etc/system-release | grep -o  " [0-9" "| cut -d "   "&NBSP;-F2"  &&  { RC1=$?; return 6; Rc2=$?; &NBSP} #os_version = ' cat /etc/system-release | grep -o  '  [0-9] "| cut -d   " "  -f2  && { RC1=$?; Rc2=$?;  }lspwdecho rc1= $RC 1echo rc2= $RC 2echo \$?=$?

4 Summary

Break and continue are all used to control the loop body.

Exit exit script Execution directly

Return is used to return the execution result of the function

When the script is executed with the source script, return will recognize it and return to the entire script execution result.


This article is from the "Self-learning Linux" blog, so be sure to keep this source http://ghbsunny.blog.51cto.com/7759574/1962255

Break continue exit return difference in Linux loop structure

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.