Linux Shell Scripting Tutorial Series (12) Shell until loop

This article is the (twelfth) part of the Linux Shell Scripting Series . For more Linux Shell tutorials, please see: Linux Shell Scripting Series Tutorials

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

 

Introduction to the Shell until loop

Shell until loop is similar to while loop, the difference is that the conditional test of while is the true value, and the until loop is the false value.

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

 

Shell until loop syntax

The syntax of the shell until loop is as follows:

until command
do
   Statement(s) to be executed until command is true
done

 

command is a conditional expression, if the return value is false, the statement in the loop body will continue to be executed, otherwise it will jump out of the loop.

 

Shell until loop usage example

Still the old rule, let's take a deeper look at the Shell until loop through an example.

 

Example 1:

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

 

Description: When the condition [a is not less than 10] is false, the command will be executed in the loop body, that is to say, when a is greater than or equal to 10, the loop body will be jumped out.

 

This script will output numbers 0-9.

 

output:

0

1

2

3

4

5

6

7

8

9

 

Example 2:

#!/bin/bash
declare -i i=10
declare -i sum=0
until ((i>10))
do
  let sum+=i
  let ++i
done
echo $sum

 

Description: This example is very similar to the example in the previous article, but the results are different. The specific analysis is left to the reader to think for himself. If you want to know the answer, you can follow the Linux University public account (WeChat: Linuxdaxue), and then send [ Shell until loop] to get the answer, or run the example yourself.

 

The content of the Shell until loop will be introduced here for you today. For more Linux Shell tutorials, please see: Linux Shell Script Series Tutorials

 

Original: Linux Shell series of tutorials (12) Shell until loop

Previous: Linux Shell Scripting Tutorial Series (11) Shell While Loop

Next: Linux Shell Scripting Tutorial Series (13) Shell branch statement case ... esac tutorial

This article is transferred from: Linux Shell Scripting Tutorial Series (12) Shell until loop

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326581187&siteId=291194637