Linux while and until loop project cases

Source: Internet
Author: User

Project Script Case

1. Determine the local area network host survival script (mainly draw judgment method, use until judgment, avoid multiple use if condition to judge)

#!/bin/bash

Declare-i i=0

Declare-i j=1

#++++++++++++++++++++ use until to determine whether user input is legitimate ++++++++++++++++++++++++++++++++++++++

until [[$netid =~ ([0-9]{1,3}\.) {3} [0-9] {1,3}]];d o #如果满足条件则退出循环, otherwise enter the loop

Read-p "Input Network (eg:192.168.0.0):" NetID

Let i++

If [$i-eq 3];then #如果3次错误输入, exit the script

echo "Input Network times out!"

Exit 1

Fi

Done

Net= ' echo $netid |cut-d.-f1-3 '

#+++++++++++++++++++++++ping function++++++++++++++++++++++++++++++++++++++++++++++++++++

Ping () { #定义函数ping的主体

While [$j-lt 255];d o

If Ping-c1-w1 $net. $j &>/dev/null;then

echo "$net. $j is Up"

Else

echo "$net. $j is Down"

Fi

Let J + +

Done

}

Ping #调用函数

2. Is it normal for every 2s monitoring website to use the while loop and call the system itself function?

#!/bin/bash

. /etc/rc.d/init.d/functions #执行系统函数调用

#+++++++++++++++++++++arg select+++++++++++++++++++++++++++++++++++++++++++++++++++++

If [$#-ne 1];then

echo $ "Usage $ URL"

Exit 1

Fi

#++++++++++++++++++++Web Monitor+++++++++++++++++++++++++++++++++++++++++++++++++++++

Flag= ' Curl-o/dev/null--connect-timeout 5-s-w "%{http_code}" $1|egrep-w "200|301|302" |wc-l "

Run () {

While True;do

If [$flag-ne 1];then

Action "is error."/bin/false #调用系统函数action, where Fales echo $? false

Else

Action "is OK."/bin/true

Fi #action函数再次调用一系列函数组成ok和fail

Sleep 2

Done

}

Run "$"

[Email protected] ~/test]#./curl.sh http://www.baidu.com
Http://www.baidu.com is OK. [OK]
Http://www.baidu.com is OK. [OK]


This article is from the "11831715" blog, please be sure to keep this source http://11841715.blog.51cto.com/11831715/1960244

Linux while and until loop project cases

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.