The shell for what? While and until contrast

Source: Internet
Author: User


Finally, came to the Shell 13 ask the last question ... Long Breath ~ ~ ~ ~ ~

The last thing to introduce is a common "loop" in shell script design.

The so-called loop is the code in the script that repeats itself under certain conditions.

The usual loop in bash shell is as follows three kinds:

* FOR
* While
* until
For loop is to read variable values from a list of lists? K "sequentially" loops to execute the command line between do and done. Cases:

For Var in one two three four five
Todo
Echo-----------
Echo ' $var is ' $var
Echo
Done
The results of the previous example will be:

1 for will define a variable called Var, whose value is one two three four five.
2 because there are 5 variable values, the command line between do and done is cycled 5 times.
3 each cycle uses echo to produce three lines of sentence. The second row is not hard quote the Magpie $var is replaced by one two three four five in turn.
4 when the last variable value is processed, the loop ends.
It's not hard to see how variable values are in the For loop. Q The number of cycles.

However, whether a variable is used in a loop or not is not necessarily dependent on the design requirements.

What if for loop?] If you use in this keyword to specify a list of variable values, the value will be inherited from $@ (or $*):

for Var; Todo
....
Done
(If you forget positional parameter, please review the 9th chapter ...)

The For loop is handy for working with lists, which can be obtained either explicitly or from positional parameter, or from variable substitution or command substitution. (Once again:? e forgot the "reorganize" feature of the command line!) )

However, for some "cumulative change" (such as Integer plus p), for also can handle:

For ((i=1;i<=10;i++))
Todo
echo "Num is $i"
Done
In addition to the for loop, we can use the while loop instead:

Num=1
While ["$num"-le 10]; Todo
echo "Num is $num"
num=$ (($num + 1))
Done
The while loop works slightly differently from the For loop:

It is not a sequential value of the variables in the list, but a fetch? Q The return value of the command line following the while

* If it is ture, execute the command between do and done, and then judge the return value after the while.
* If False, no longer executes the command between do and do to end the loop.
Analysis of the previous example:

1 define the variable num=1 before the while.
2 then test whether the $num is less than or equal to 10.
3) The result is true, and the Echo is executed. K adds the value of NUM to one.
4) Second Test, at which time the value of NUM is 1+1=2, still less than or equal to 10, so true, continue the loop.
5) The test will not be lost until Num is 10+1=11. Hellip; So the loop ends.
It is not difficult to find:

* If the test result of the while is always true, the loop will always execute forever:

While:; Todo
Echo Looping ...
Done
The example ":" is the null command for bash, with no action except return value that returns TRUE. So this cycle does not end, called a dead loop.

The generation of dead loops may be intentionally designed (such as running daemon), or it may be a design error.

To end a dead loop, you can terminate it through signal (such as pressing CTRL-C).

(about process and signal, wait for the opportunity to add later, 13 ask temporarily skip.) )

Once you can understand the while loop, you can understand the until loop

* Contrary to while, until enters the loop when return value is false or ends.
Therefore, the previous example we can easily use until to write:

Num=1
Until [! "$num"-le 10]; Todo
echo "Num is $num"
num=$ (($num + 1))
Done
Or:

Num=1
Until ["$num"-GT 10]; Todo
echo "Num is $num"
num=$ (($num + 1))
Done
Okay, the three commonly used loop about Bash is temporarily introduced here.

Before I conclude this chapter, we'll have two loop-related commands:

Break
Continue
These two commands are commonly used in a composite loop, that is, in the do ... and there is a further layer of loop, of course, used in a single cycle is also all right ... ^_^

Break is used to interrupt the cycle, which is "?? Forced to end the cycle.

If you specify a value of n after the break, then "from Inside Out" interrupts the nth loop, and the default value is break 1, which interrupts the current loop.

When you use break you need to be aware that it is different from return and exit:

* Break is end loop
* Return is the End function
* Exit is End Script/shell
And Continue is the opposite of break: Force into the next cyclic action.

If you don't understand it, you can simply see that the sentence between continue and done is skipped and returns to the top of the loop ...

The same as the break: Continue can also be specified after a value of N, to? The loop in which the Q set continues (from the Inside Out), the default value is continue 1, which is the continuation of the current loop.

The use of loop in shell script design can greatly improve the processing ability of script under complex conditions.

Please practice more ....

———–

Well, it's time to end it.

What are you talking about? The base concept of the shell is not to tell everyone the "answer", but to bring the "inspiration" ...

In the future discussion of the shell, I may often use the "link" way to guide the return of the 13 question? Jes, the 诮 of the dust, and the ┨ of the North Shun four of the cuttings. Restore the bad inquiry Pixy roll ah ⑼ pepper throat α5??? M??? The oath?? 愀 sigh is the 肜 rank ぃ liao plaque? The bottom-arm alcohol from 罾 hacking 狻?/p>

Yes, I attach great importance to "practice" and "independent thinking" these two learning elements, if you can grasp the true meaning, let me say:

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.