Shell Loop Loops

Source: Internet
Author: User

1. Cycle
The shell loops and executes a program continuously until the condition is met. Loops are divided into 4 types, while loops, until loops, for fixed processing, for numeric processing.
2. While loop
The while loop is executed until the condition is not met before stopping. Syntax: while [condition]doDo somethingdone example: Loop until user input is correct
#!/bin/bash# desc:while loopwhile ["$yn"! = "Yes"-a "$yn"! = "yes"] do     read-p "Please input yes/yes to stop:" Yndoneecho "OK"
Execution Result:
Example: from 1 to 100
#!/bin/bash# desc:while loopsum=0i=0while [$i-lt]do    i=$ (($i + 1));    
3. Until cycle
Until loops and while instead, the loop is terminated when the condition condition is established. Syntax: until [condition]doDo Somethingdone Example:
#!/bin/bash# desc:while loopuntil ["$yn" = = "Yes"-o "$yn" = = "Yes"]do    read-p "Please input yes/yes to stop:" Y Ndoneecho "OK"
Perform:
[Email protected] sh]$ sh while.sh Please input yes/yes to Stop:hi         

4. For Fixed loop
For is a loop of known number of times. Syntax: for var in con1 Con2. DoDo Somethingdone Example:
#!/bin/bash# desc:for loopfor animal in dog cat pig does    echo "HI, ${animal}" done
Perform:
[[Email protected] sh]$ sh for.sh HI, Doghi, Cathi, pig[[email protected] sh]$
Example: Current catalog file
#!/bin/bash# desc:for loopfilelist=$ (LS) for filename in $filelistdo    echo $filenamedone

5. For-Value loops
For numeric loops, suitable for cyclic numerical calculations. Syntax for (initial value; limit value; step)) doDo Somethingdone Example:
#!/bin/bash# desc:for loopsum=0for ((i=0; i<=100; i++)) do    sum=$ (($sum + $i)) Doneecho ' sum is: ' $sum

Address: http://blog.csdn.net/yonggang7/article/details/40679701

Shell Loop Loops

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.