Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-01-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report