while及until循环结构
while CONDITION;do
statement
done
进入循环:条件满足
退出循环:条件不满足
当需要命令的执行状态返回值时,可以直接把整个命令当做循环的条件
until CONDITION;do
statement
...
done
进入循环:条件不满足
退出循环:条件不满足
退出循环的控制关键词
break:提前退出循环,将不再执行循环
continue:提前结束本轮循环,而进入下一轮循环
:|true:表示为死循环(永远为真)
Example1:
#!/bin/bash
declare -i sum=0
declare -i i=0
while ((i<100));do #此处必须小于100,否则会多出i=101
let i++
if [ $[$i%2] -eq 0 ];then
continue
fi
let sum+=i
done
echo "$sum"
计算100以内奇数的和
Example2:
#!/bin/bash
declare -i sum=0
for i in {1..1000};do
let sum+=i
if [ $sum -gt 5000 ];then
break
fi
done
echo "$i"
echo "$sum"
当sum的值大于5000时,退出整个循环
Example3:
#!/bin/bash
while :;do
read -p "please input file: " FILE
[ $FILE == 'quit' ] && break
if [ -e $FILE ];then
echo "$FILE exists"
else
echo "no $FILE file"
fi
done
echo "quit..."
while的特殊用法
while read LINE;do
循环体
done < /PATH/TO/SOMEFILE
通过read命令读取文件中的每一行,每次读取一行,读取一行循环一次,并且把每一行的内容赋值给LINE变量,可以通过循环体对LINE变量进行处理
Example:
#!/bin/bash
FILE=/etc/passwd
declare -i i=0
while read LINE;do
[ `echo $LINE | awk -F: '{print $7}'` == '/bin/bash' ] && echo `echo $LINE | awk -F: '{print $1}'` && let i++
[ $i -eq 6 ] && break
done < $FILE
读取文件中的一行,把一行的内容赋值给LINE,对LINE进行判断,如果是/bin/bash,就显示用户名,并且i的值+1,当i的值等于6的时候,退出循环;不能把 let i++和前面的判断分开,否则判断不成功i的值也会加1;i=6时退出循环
示例
Example1:
#!/bin/bash
read -p "Input something: " STRING #通过read -p跟上想要打印的提示信息,后面输入的参数将被赋值给STRING这个变量;read -p也可以配合case
while [ $STRING != 'quit' ];do
echo "$STRING" | tr 'a-z' 'A-Z'
read -p "Again input something: " STRING
done
输出一个变量,变量值不等于quit时进入循环,大小写替换,然后再输出变量值,判断变量值是否等于不等于quit,不等于以此类推继续循环,等于quit则退出循环
Example2:
#!/bin/bash
until who|grep "hadoop" &>/dev/null;do
echo "hadoop is no sign-in"
sleep 5
done
echo "hadoop is log in"
判断条件中的命令是否是对的,当命令不对的时候进入循环,显示打印一行字和sleep 5秒,然后再判断命令是否错误,错误继续循环,命令正确则退出循环
Example3:
#!/bin/bash
who|grep "hadoop" &>/dev/null
RETURN=$?
until [ $RETURN -eq 0 ];do
echo "hadoop is no sign-in"
sleep 5
who|grep "hadoop" &>/dev/null
RETURN=$?
done
echo "hadoop is log in"
Example2是对命令的执行状态进行判断,这个是取出命令执行状态的返回值进行条件判断
Example4:
#!/bin/bash
declare -i sum=0
declare -i i=1
while ((i<=100));do
let sum+=i #sum+=i = sum=$[$sum+$i]
let i++
done
echo "$sum"
i初始值为1,判断i是小于等于100,执行sum+=i,执行完后让i的值加1,i=2;再次判断i是否小于等于100,满足继续执行循环,以此类推;不满足则退出循环
Example5:
#!/bin/bash
while ! bash -n $1 &>/dev/null ;do
read -p "Scripts syntax error,Please input any key edit scripts or q|Q quit: " SCRIPT
case $SCRIPT in
q|Q)
echo "quit edit..."
exit 8
;;
*)
vim $1
;;
esac
done
检查脚本,脚本语法错误时将进入循环;当输入q或者Q时,退出脚本;当输入任意键则vim编辑脚本,脚本修改完后会继续循环检查脚本语法,如果脚本语法还有错误,将继续执行退出或者编辑;脚本语法无错误时,将不进入循环
for循环的C语言风格形式
for循环基本形式:
for 变量;do
循环体
done
for循环的C语言风格形式:
for (( expr1 ; expr2 ; expr3 )); do
循环体
done
expr1指定变量的初始值、expr2判定何时退出循环、expr3修正变量的值
Example1:
#!/bin/bash
declare -i SUM=0
for (( I=2;I<=100;I+=2));do I+=2 = I=$[$I+2]
let SUM+=$I SUM+=$I = SUM=$[$SUM+$I]
done
echo "$SUM"
I变量的初始值为2,2是小于等于100,执行循环体;执行完本次循环后,执行I+=2,得出的数是4,判断4是小于等于100的,再次执行循环体,以此类推,得出100内偶数相加的和是多少
Example2:
用while循环、until循环、for循环(两种方法)通过ping命令测试192.168.0.151——192.168.0.254之间的所有主机是否在线;在线显示up,不在线显示down
for(第一种方法):
#!/bin/bash
for i in {151..254};do
if ping -c 1 -W 1 192.168.0.$i &>/dev/null;then #-c表示测试1次,-W表示超时时长,以秒为单位
echo "192.168.0.$i is up"
else
echo "192.168.0.$i is down"
fi
done
for(第二种方法):
#!/bin/bash
for (( i=151;i<=254;i++ ));do
ping -c 1 -W 1 192.168.0.$i &>/dev/null
PIN=$?
if [ ! $PIN -eq 0 ];then
echo "192.168.0.$i is down"
else
echo "192.168.0.$i is up"
fi
done
while:
#!/bin/bash
declare -i I=151
while ((I<=254));do
ping -c 1 -W 1 192.168.0.$I &>/dev/null
PIN=$?
if [ $PIN -eq 0 ];then
echo "192.168.0.$I is up"
else
echo "192.168.0.$I is down"
fi
let I++
done
while:(第二种用法)
#!/bin/bash
read -p "Please input number: " I
while [ $I -le 254 ];do
if ping -c 1 -W 1 192.158.0.$I &>/dev/null;then
echo "192.168.0.$I is up"
else
echo "192.168.0.$I is down"
fi
let I++
done
until:
#!/bin/bash
declare -i I=151
until [ $I -gt 254 ];do
if ping -c 1 -W 1 192.168.38.$I &>/dev/null;then
echo "192.168.38.$I is up"
else
echo "192.168.38.$I is down"
fi
let I++
done