linux while和until循环项目案例
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了linux while和until循环项目案例相关的知识,希望对你有一定的参考价值。
项目脚本案例
1. 判断局域网主机存活脚本(主要吸取判断方法,利用until判断,避免多次使用if条件判断)
#!/bin/bash declare -i i=0 declare -i j=1 #++++++++++++++++++++利用until判断用户输入是否合法++++++++++++++++++++++++++++++++++++++ until [[ $netid =~ ([0-9]{1,3}\.){3}[0-9]{1,3} ]];do #如果满足条件则退出循环,否则进入循环 read -p "Input network(eg:192.168.0.0): " netid let i++ if [ $i -eq 3 ];then #如果3次错误输入,则退出脚本 echo "Input network times out!" exit 1 fi done net=`echo $netid|cut -d. -f1-3` #+++++++++++++++++++++++ping function++++++++++++++++++++++++++++++++++++++++++++++++++++ Ping () { #定义函数ping的主体 while [ $j -lt 255 ];do 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.利用while循环及调用系统本身function,每2s监控网站是否正常?
#!/bin/bash . /etc/rc.d/init.d/functions #执行系统函数调用 #+++++++++++++++++++++arg select+++++++++++++++++++++++++++++++++++++++++++++++++++++ if [ $# -ne 1 ];then echo $"usage $0 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 "$1 is error." /bin/false #调用系统函数action,其中fales时echo $? 假 else action "$1 is ok." /bin/true fi #action函数再次调用一系列函数组成ok和fail sleep 2 done } run "$1" [[email protected] ~/test]#./curl.sh http://www.baidu.com |
本文出自 “11831715” 博客,请务必保留此出处http://11841715.blog.51cto.com/11831715/1960244
以上是关于linux while和until循环项目案例的主要内容,如果未能解决你的问题,请参考以下文章
for / while / until 基础循环语句案例图解 shell编程之循环语句
宝贝,我半夜在输液,输的是想你的夜(循环语句for,while,until附带date,含案例),