The while loop of the shell script

Source: Internet
Author: User

Directory:
I. While loop and until loop
Two. Loop control statement continue break shift
Three. Special usage

I. While loop and until loop
  • 1.while Cycle
    While CONDITION; Do
    Loop body
    Done
    CONDITION: cyclic control condition; Before entering the loop, make a judgment first; once again, each cycle is judged again; if the condition is true, a loop is executed until the condition test state is a false termination loop.
  • 2.until
    Until CONDITION; Do
    Loop body?
    Done
    The Until loop is opposite to the while loop condition: The loop control condition is false, then a loop is executed, until the condition test state is true to terminate the loop.
    Example: Displaying 1-5 of the numbers

                    #!/bin/bash                      n=0                    while [ $n -lt 5 ];do                         let  n=$n+1                        echo $n                   done
  • Until notation
    #!/bin/bash
    N=0
    Until [$n-eq 5];d o
    Let n= $n +1
    Echo $n
    Done two. loop control statements

    Continue break shift
    * 1.continue[n]: End of the first N layer of the current cycle, and directly into the next round of judgment; the inner layer is the 1th floor.
    While CONDTIITON1; Do
    CMD1 ...
    if CONDITION2; Then
    Continue
    Fi
    Cmdn
    ...
    Done

  • 2.break [n]: Early end of the nth layer cycle, the inner layer is the 1th layer
    While CONDTIITON1; Do
    CMD1 ...
    if CONDITION2; Then
    Break
    Fi
    Cmdn
    ...
    Done

    Let's make a few changes to the first example in which we add continue and break
    #!/bin/bash
    N=0
    While [$n-lt 5];d o
    Let n= $n +1
    If [$n-eq 3];then
    Continue
    Fi
    Echo $n
    Done

  • Output of 1245 when adding continue

                 #!/bin/bash        n=0   while [ $n -lt 5 ];do           let  n=$n+1        if [ $n -eq 3 ];then           break        fi    echo $n   done
  • The result of joining break is 12
    Thus it can be concluded that the difference between the two is that continue is the end of the wheel cycle
    Break is the end when the layer loops
    3.shift [n] is used to move the list of parameters to the left for a specified number of times, and the default is to move left once. Parameter list once moved, the left-most argument is removed from the list.

The while loop of the shell script

Related Article

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.