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