Shell loop syntax

Source: Internet
Author: User
While do done, until do done (indefinite loop)

In general, the most common status of an indefinite loop is the following two states:

 
While[Condition] <=The status in the brackets is the limit type.Do<=DoIt is the beginning of the loop!ProgramSectionDone<=DoneIs the end of the loop

While Chinese is "when...", so this method is 『When the condition is set, a loop is performed until the condition of the condition is not set..

 

There is another way to change the loop:

Until[Condition]DoProcedure SectionDone

This method is the opposite of while 『When the condition is set, the loop is terminated. Otherwise, the loop is continued.Is it the opposite?

 

Let's make a simple exercise with while. If you want the user to enter yes or yes to end the program running, otherwise, the user will be notified of the input string.

[Root @ WWW Scripts] # vi sh13. Sh  # ! /Bin/ Bash # program: # repeat question  Until  User input correct answer. # History :# 2005 / 08 / 29  Vbird first releasepath =/Bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin :~ / Binexport path  While [ "  $ YN  " ! = "  Yes  " - "  $ YN " ! = "  Yes  "  ]  Do  Read -P "  Please input Yes/yes to stop this program:  "  YN  Done  Echo   "  OK! You input the correct answer.  " 

 

The above example shows that "when the $ YN variable is not" yes "and $ YN is not" yes ", the program in the circle will be carried out .』 If $ YN is "yes" or "yes", it will leave the loop ~ What if until is used? Haha interesting Luo ~ His condition will become like this:

[Root @ WWW Scripts] # vi sh13- 2 . Sh  # ! /Bin/ Bash # program: # repeat question  Until  User input correct answer. # History :#  2005 / 08 / 29  Vbird first releasepath =/Bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin :~ /Binexport path  Until [ "  $ YN  " = "  Yes  " -O "  $ YN  " = "  Yes  "  ]  Do  Read -P "  Please input Yes/yes to stop this program:  "  YN  Done  Echo   "  OK! You input the correct answer.  " 

 

What are the differences between the two items! ^_^ Again. What if I want to calculate the data of 1 + 2 + 3 +... + 100? Use the loop ~ He is like this:

[Root @ WWW Scripts] # vi sh14. Sh  # ! /Bin/ Bash # program: # Use loop to calculate "  1 + 2 + 3 +... + 100  "  Result. # History :#  2005 / 08 / 29  Vbird first releasepath =/Bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin :~ / Binexport paths = 0  # This is the total numeric variable I = 0 # This is a cumulative value, that is 1 ,2 , 3  ....  While [ "  $ I  " ! = "  100  "  ]  Do  I = $ ($ I + 1 ) # Every time I is added 1  S = $ ($ S +$ I) # Add the total number once every time!  Done  Echo   "  The result of '1 + 2 + 3 +... + 100 'is => $ s  " 

 

For... do... done (fixed loop)

His syntax is:

 
ForVaRInCon1 con2 con3...DoProgram SectionDone

In the preceding example, when the variable content of $ VaR is working in the Loop:

    1. For the first round, the content of $ VaR is con1;
    2. For the second round, the content of $ VaR is con2;
    3. For the third round, the content of $ VaR is con3;
    4. ....

We can make a simple exercise. Suppose I have three animals: dog, cat, and elephant. If I want to output the following words in each row: "There are dogs...", you can:

[Root @ WWW Scripts] # vi sh15. Sh  # ! /Bin/ Bash # program: # using  For ... Loop to print 3  Animals # History :#  2005 / 08 / 29  Vbird first releasepath =/Bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin :~ / Binexport path  For Animal In Dog Cat  Elephant  Do      Echo   "  There are $ {animal} s ....  "  Done 

 

The domain I want to detect is the host's 192.168.1.1 ~ 192.168.1.100, because there are 100 hosts, I will not enter 1 to 100 after? You can do this now!

[Root @ WWW Scripts] # vi sh17. Sh # ! /Bin/ Bash # Program # Use  Ping Command to check the network '  S PC state.  # History #  2009 / 02 / 18  Vbird first releasepath =/Bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin :~ / Binexport pathnetwork = "  192.168.1 "  # First define the first part of a domain!  For Sitenu In $ ( Seq   1   100 )# Seq  Abbreviation of Sequence  Do  # Obtain  Ping  Is the return value correct or failed!  Ping -C 1 -W   1 $ {Network}. $ {sitenu} &>/dev/ Null & Result = 0 | Result = 1  # Start to show whether the result is correct start (up) or wrong no connection (down)  If [ "  $ Result  " = 0 ]; Then                  Echo   " Server $ {network}. $ {sitenu} is up.  "          Else                  Echo   "  Server $ {network}. $ {sitenu} is down.  "          Fi  Done 

 

What should I do if I want the user to enter a directory file name and find out the permission for the file name in a directory?

[Root @ WWW Scripts] # vi sh18. Sh  # ! /Bin/ Bash # program: # user input  Dir Name, IFind  The permission of files. # History :#  2005 / 08 / 29  Vbird first releasepath =/Bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin :~ / Binexport path #  1  . Check whether the directory exists first? Read -P "  Please input a directory:  "   Dir  If [ "  $ Dir  " = "" -O! -D "  $ Dir  " ]; Then      Echo   "  The $ DIR is not exist in your system.  "  Exit  1  Fi  # 2  . Start the test file ~ Filelist = $ ( Ls $ Dir  ) # List all file names in this directory  For Filename In  $ Filelist  Do  Perm = ""  Test -R "  $ DIR/$ filename  " & Perm ="  $ Perm readable  "  Test - W   "  $ DIR/$ filename  " & Perm = "  $ Perm writable  "  Test -X "  $ DIR/$ filename  " & Perm = " $ Perm executable  "      Echo   "  The file $ DIR/$ filename's permission is $ perm  "  Done 

 

For... do... done numerical processing

In addition to the above method, there is another way to write for loop! Syntax:

 
For(Initial value; Restriction value; running step ))DoProgram SectionDone

This syntax is suitable for numeric operations. The content of the three strings following the for clause indicates:

    • Initial Value: the initial value of a variable in the loop, which is directly configured with something similar to I = 1;
    • Limit Value: when the value of a variable is within the limit value, the loop continues. For example, I <= 100;
    • Running steps: the variable changes every time a loop is made. For example, I = I + 1.

It is worth noting that in the configuration of "Running step", if you add 1 at a time, you can use a method similar to "I ++, that is to say, I adds one for every loop. Okay. In this way, we can accumulate 1 to the loop of user input!

[Root @ WWW Scripts] # vi sh19. Sh  # ! /Bin/ Bash # program: # Try  Do Calculate 1 + 2 +... + $ {Your_input} # History :#  2005 / 08 /29  Vbird first releasepath =/Bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin :~ / Binexport pathread -P "  Please input a number, I will count for 1 + 2 +... + your_input:  "  Nus = 0  For (I = 1 ; I <= $ Nu; I = I + 1  ))  Do S = $ ($ S + $ I ))  Done  Echo   "  The result of '1 + 2 + 3 +... + $ nu 'is ==> $ s  " 

 

From http://vbird.dic.ksu.edu.tw/linux_basic/0340bashshell-scripts_5.php#loop

 

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.