Shell中的until与while
ฅ'ω'ฅ♪

Shell中的until与while

while命令

命令格式 while CONDITION;do 循环体 循环变量修正表达式 done 当CONDITION为真时,执行循环 当CONDITION为假时,退出循环

命令使用 i.e.使用while分别实现192.168.0.0/24 网段内,地址是否能够ping通,弱ping通则输出"success!",若ping不通则输出"fail!"

#!/bin/bash
let count=1
while [ $count -lt 255 ];do
temp_ip="192.168.0.$count"
ping -w 3 -c 3 -q $temp_ip &> /dev/null
[ $? -eq 0 ] && echo "Success to ping $temp_ip"|| echo "Fail to ping $temp_ip"
let count++
done
echo "Complete"
[bhps@test ~]$ sh test.sh
Fail to ping 192.168.0.1
Fail to ping 192.168.0.2
Fail to ping 192.168.0.3

until

命令格式 until CONDITION;do 循环体 循环控制变量修正表达式 done 当CONDITION为假时,执行循环 当CONDITION为真时,退出循环

命令使用 i.e.使用until分别实现192.168.0.0/24 网段内,地址是否能够ping通,弱ping通则输出"success!",若ping不通则输出"fail!"

#!/bin/bash
let count=1
until [ $count -ge 255 ];do
temp_ip="192.168.0.$count"
ping -w 3 -c 3 -q $temp_ip &> /dev/null 
[ $? -eq 0 ] && echo "Success to ping $temp_ip"|| echo "Fail to ping $temp_ip"
let count++
done 
echo "Complete"
[bhps@test ~]$ sh test.sh
Fail to ping 192.168.0.1
Fail to ping 192.168.0.2
Fail to ping 192.168.0.3
CANCEL

-评论-

Here you can post what you want to say, if you have more information please contact me by the following way.

-昵称-
-QQ-
-邮箱-
想说些什么?
-SUBMIT-

-电联 Phone-

+86 18520664652

-邮箱 Email-

boogieLing_o@163.com

boogieLing_o@qq.com

Your name. OS platform Browser model

What do you want to say?

created time

游說萬乘苦不早,著鞭跨馬涉遠道。

阿凌的貓爬架

幸會,

激活Ubuntu

转到“设置”以激活Ubuntu。

R0's board.