Get the App
SLTechnology News&Howtos  ›  Servers  › 

Carding the basic knowledge of shell script & lt; VII & gt;: process Control until

Shulou Source: shulou.com Published: 2022-06-02 07:19:04 10月01日 Update

Until syntax

The until loop executes a series of commands until the condition is true.

The until loop and the while loop are handled in the opposite way.

While loops are generally better than until loops, but until loops are more useful at some times-and only in rare cases.

Until syntax format:

Until test condition

Do

Instruction

Done

Condition is generally a conditional expression, and if the return value is false, the statement inside the loop continues to be executed, otherwise it jumps out of the loop.

Example 1: for the following example, we use the until command to output numbers from 0 to 9:

#! / bin/bash

Aplasy 0

Until [! $a-lt 10]

Do

Echo $a

A=expr $a + 1

Done

Output 0 1 2 3 4 5 6 7 8 9

Example 2

#! / bin/bash

#

# determine whether ip can reach ping. If not, wait for 60s.

Read-p "Enter IP Address:" ipadd

Until ping-c 1$ ipadd & > / dev/null

Do

Echo "${ipadd} ping."

Sleep 60

Echo "waited for 60s"

Done

Execution result

[root@localhost shell] # sh until-1.sh

Enter IP Address:192.168.1.123

192.168.1.123 ping.

Waited for 60s

192.168.1.123 ping.

Waited for 60s

Example 3

#! / bin/bash

#

# determine whether the user is in the system

Username=$1

If [$#-lt 1]

Then

Echo "Usage:'basename $0' []"

Exit 1

Fi

If grep "^ $username:" / etc/passwd > / dev/null

Then

:

Else

Echo "$username is not a user on this system."

Exit 2

Fi

Until who | grep "$username" > / dev/null

Do

Echo "$username is not logged on."

Sleep 5

Done

Execution result

[root@localhost shell] # sh until-2.sh

Usage:'basename until-2.sh' []

[root@localhost shell] # sh until-2.sh ab

Ab is not a user on this system.

Tags: Loop instance condition result command syntax output just loop body situation instruction number way time useful format user system expression statement Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn Shulou Information NVidia Shulou Technology Docker