Linux Shell series (12) Shell until loop, shelluntil

Source: Internet
Author: User

Linux Shell series (12) Shell until loop, shelluntil

This article is Part 12 of the Linux Shell series tutorials. For more Linux Shell tutorials, see the Linux Shell series tutorials.

In the last two articles: Linux Shell series (10) Shell for loop and Linux Shell series (11) Shell while loop, we have introduced the for loop and while loop of Shell loop statements in detail. This article introduces the last loop statement in Shell: Shell until loop.

Introduction to Shell until Loop

The Shell until loop is similar to the while loop. The difference is that the while condition test is to test the true value, while the until loop is to test the false value.

That is to say, in the while LOOP, if the condition test result is true (the return value is 0), it enters the loop; In the until loop, if the condition test result is true (the return value is 0 ), the loop jumps out. If the test result is false (the return value is not 0), the loop continues.

Shell until loop syntax

The syntax of Shell until loop is as follows:

until commanddo   Statement(s) to be executed until command is truedone

Command is a conditional expression. If the returned value is false, the statement in the loop body is executed. Otherwise, the loop jumps out.

Example of Shell until Loop

It's still the old rule. Let's look at the Shell until loop through examples.

Example 1:

#!/bin/basha=0until [ ! $a -lt 10 ]do   echo $a   a=`expr $a + 1`done

Note: When the condition "a is not less than 10" is false, the command will be executed cyclically in the loop body. That is to say, when a is greater than or equal to 10, the loop body will jump out.

This script outputs 0-9 numbers.

Output:

0

1

2

3

4

5

6

7

8

9

Example 2:

#!/bin/bashdeclare -i i=10declare -i sum=0until ((i>10))do  let sum+=i  let ++idoneecho $sum

Note: This example is similar to the example in the previous article, but the results are different. You can leave the specific analysis to your readers. If you want to know the answer, follow the public account of Linux University (No: linuxdaxue), and then send the [Shell until loop] to obtain the answer, or run the example by yourself.

For more information about the Shell until loop, see the Linux Shell series tutorials.

  • Copyright:This site's original article, published four months ago by the Linux University (Linuxdaxue.com), contains 948 words.
  • Reprinted Please note:Linux Shell series (12) Shell until loop | Linux University + copy Link

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.