70.for循环while循环break跳出循环continue结束本次循环exit
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了70.for循环while循环break跳出循环continue结束本次循环exit相关的知识,希望对你有一定的参考价值。
for循环
语法:for 变量名 in 条件; do …; done
for 会以空格或者回车为分隔符
案例1
#!/bin/bash
sum=0
for i in `seq 1 100`
do
sum=$[$sum+$i]
echo $i
done
echo $sum
文件列表循环
#!/bin/bash
cd /etc/
for a in `ls /etc/`
do
if [ -d $a ]
then
ls -d $a
fi
done
while循环
语法 while 条件; do … ; done
- 案例1
#!/bin/bash while true do load=`w|head -1|awk -F ‘load average: ‘ ‘{print $2}‘|cut -d. -f1` if [ $load -gt 10 ] then /usr/lib/zabbix/alertscripts/mail.py [email protected] "load is high" "load is high: $load" fi sleep 30 done
- 案例2
#!/bin/bash
while :
do
read -p "Please input a number: " n
if [ -z "$n" ]
then
echo "you need input sth."
continue
fi
n1=`echo $n|sed ‘s/[0-9]//g‘`
if [ -n "$n1" ]
then
echo "you just only input numbers."
continue
fi
break
done
echo $n
break跳出循环
#!/bin/bash
for i in `seq 1 5`
do
echo $i
if [ $i == 3 ]
then
break
fi
echo $i
done
echo aaaaaaa
continue结束本次循环?
忽略continue之下的代码,直接进行下一次循环
#!/bin/bash
for i in `seq 1 5`
do
echo $i
if [ $i == 3 ]
then
continue
fi
echo $i
done
echo $i
exit直接退出脚本
#!/bin/bash
for i in `seq 1 5`
do
echo $i
if [ $i == 3 ]
then
exit
fi
echo $i
done
echo aaaaaaa
以上是关于70.for循环while循环break跳出循环continue结束本次循环exit的主要内容,如果未能解决你的问题,请参考以下文章
如果有多个循环,在最内层里用break语句是是跳出最内层循环还是跳出所有循环