How to use for while until in shell script Programming

Source: Internet
Author: User

 

Finally, we will introduce the common loop in shell script design ). The so-called loop is a piece of code in the script that is repeatedly executed under certain conditions.
There are three types of commonly used loop in bash shell:
*
* While
*
A For Loop reads the variable values from a list and runs the command line between do and done in an "sequential" loop.
Example:
For VaR in one two three four five
Do
Echo -----------
Echo '$ VaR is' $ VaR
Echo
Done
The execution result of the above example will be:
1) For defines a variable named var whose values are one two three four five in sequence.
2) because there are five variable values, the command line between do and done is cyclically executed five times.
3) three sentences are generated by ECHO in each loop.
The $ VaR in the second row is replaced with one two three four five in sequence.
4) when the last variable value is processed, the loop ends.
We can easily see that in a for loop, the number of variable values determines the number of cycles. However, the use of variables in a loop is not necessarily dependent on the design requirements. If for loop does not use the in keyword to specify the variable value list, its value will be inherited from $ @ (or $:
For var; do
....
Done
(If you forget positional parameter, please review 9th and ask ...)
For Loop is very convenient for processing "list" items. The list can be explicitly specified or obtained from positional parameter,
You can also get it from variable replacement or command replacement... (another reminder: Do not forget the "restructuring" feature of the command line!) However, for some "accumulative changes" items (such as integer addition and subtraction ),
Can also handle:
For (I = 1; I <= 10; I ++ ))
Do
Echo "num is $ I"
Done
In addition to the for loop, we can also use the while loop as follows:
Num = 1
While ["$ num"-le 10]; do
Echo "num is $ num"
Num = $ ($ num + 1 ))
Done
The principle of while loop is slightly different from that of for loop: it does not process the variable values in the list one by one, but depends on the return value of the command line after while:
* If it is true, execute the command between do and done and re-judge the return value after while.
* If the value is false, the loop ends without executing the command between do and done.
Analysis example:
1) before while, define the variable num = 1.
2) then test whether (TEST) $ num is less than or equal to 10.
3) if the result is true, execute echo and add one to the num value.
4) perform the second round of testing. At that time, the value of num is 1 + 1 = 2, which is still less than or equal to 10. Therefore, it is true and the loop continues.
5) The test will not fail until num is 10 + 1 = 11... so the loop ends.
We can easily find that:
* If the while test result is always true, the loop will be executed permanently:
While:; do
Echo looping...
Done
In the preceding example, ":" Is the bash NULL command. It does not take any action, except to return the true return value.
. Therefore, this loop will not end. It is called an endless loop. The creation of an endless loop may be intentionally designed (such as running daemon), or it may be a design error. To end the endless search loop, you can
To terminate (for example, press Ctrl-C ).
(Process and signal should be supplemented later. Question 13 is skipped for the time being .)
Once you can understand the while loop, you can understand the until loop:
* Opposite to while, until enters the loop when return value is false, otherwise it ends.
Therefore, the previous example can also be easily written using:
Num = 1
Until [! "$ Num"-le 10]; do
Echo "num is $ num"
Num = $ ($ num + 1 ))
Done
Or:
Num = 1
Until ["$ num"-GT 10]; do
Echo "num is $ num"
Num = $ ($ num + 1 ))
Done
Okay: Here are three commonly used bash loop.
Before closing shell 13th, I would like to add two loop-related commands:
* Break
* Continue
These two commands are often used in a compound loop, that is, there is a further layer of loop between do... done. Of course, they have never been used in a single loop.
The break is used to interrupt the loop, that is, the "forced end" loop. If a value of N is specified after the break, the nth cycle is interrupted "from the inside out". The default value is break 1, that is, the current loop is interrupted.
When using break, note that it is different from return and exit:
* Break is the end loop.
* Return is the end function.
* Exit is the end script/Shell
In contrast to break, continue forces the next loop. If you cannot understand it, you can simply think of it as: From continue to done
Returns the top of the loop after skipping the sentence between them... the same as break: A value N can be specified after continue.
To determine which layer (calculated from the inside out) to continue. The default value is continue 1, that is, to continue the current loop.
In shell script design, loop can greatly improve the processing capability of scripts under complicated conditions.
Please exercise more ....


-----------
Well, it's time to end.
My mother-in-law told everyone about a bunch of basic shell concepts,
The purpose is not to tell everyone "the answer", but to give everyone "inspiration "...
In future discussions about shell, I may often use the "Link" method to guide the content in question 13,
So that we can have some discussions with each other during the technical discussion, instead of talking about each other and making every effort.
However, we hope that the 13th question will bring you more thoughts and fun. The most important thing is to deepen your understanding through practice.

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.