Shell loop, shellloop

Source: Internet
Author: User

Shell loop, shellloop
1. Loop
The shell loop continues to execute a program until it meets the conditions. There are four types of loops: while LOOP, until loop, for fixed processing, and for numerical processing.
2. while Loop
The while loop is executed until the condition does not match. Syntax: while [condition] do // do 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:
[work@www sh]$ sh while.sh Please input yes/YES to stop : noPlease input yes/YES to stop : yesOK[work@www sh]$ 
For example, from 1 to 100
#!/bin/bash# desc : while loopsum=0i=0while [ $i -lt 100 ]do    i=$(($i+1));    sum=$(($sum+$i))doneecho "From 1 to 100, sum is : " $sum 
3. until Loop
The until loop is opposite to the while loop. When the condition is set, the loop is terminated. Syntax: until [condition] do // do somethingdone example:
#!/bin/bash# desc : while loopuntil [ "$yn" == "yes" -o "$yn" == "YES" ]do    read -p "Please input yes/YES to stop : " yndoneecho "OK"
Run:
[work@www sh]$ sh while.sh Please input yes/YES to stop : hi         Please input yes/YES to stop : yesOK[work@www sh]$ 

4. for fixed Loop
For is a known number of cycles. Syntax: for var in con1 con2 .. do // do somethingdone example:
#!/bin/bash# desc : for loopfor animal in dog cat pig do    echo "HI, ${animal}"done
Run:
[work@www sh]$ sh for.sh HI, dogHI, catHI, pig[work@www sh]$
Example: Current directory file
#!/bin/bash# desc : for loopfilelist=$(ls)for filename in $filelistdo    echo $filenamedone

5. for numeric Loop
For numeric loop, applicable to cyclic numerical calculation. Syntax for (initial value; limit value; step size) do // do 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

Why are there errors in the for loop in shell?

#! /Bin/sh
X = 1
Y = 2
For (I = 1; I <$1; I ++ ))
Do
M = $ y
Let y = $ x + $ y
X = $ m
Done
Echo $ x/$ y

Why are two parentheses for in shell?

For loop syntax has the following two types:

(1) This is the classic for usage of shell:
For varname [in word...]
Do
...
Done

Example:
For I in a B c
Do
Echo $ I
Done

(2) This syntax is similar to the C/C ++ usage, and (...) is used for arithmetic operations in shell:

For ([init_expression]; [loop_condition]; [loop_expression])
Do
...
Done

Example:
For (I = 0; I <5; I ++ ))
Do
Echo $ I
Done

Note: This for usage is only supported by ksh after 1988-11-16, so many Unix systems do not support this for syntax. For example, HPUX or ibm aix.

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.