shell脚本 for循环break跳出循环continue结束本次循环
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了shell脚本 for循环break跳出循环continue结束本次循环相关的知识,希望对你有一定的参考价值。
20.10 for循环
- 语法:for 变量名 in 条件; do ...; done
;案例1
[[email protected] shell]# cat for.sh
#!/bin/bash
sum=0
for i in `seq 1 100`
do
sum=$[$sum+$i]
done
echo $sum
#输出的结果
[[email protected] shell]# sh for.sh
5050
- 文件列表循环
[[email protected] shell]# cat for2.sh
#!/bin/bash
cd /etc/
for a in `ls /etc/`
do
if [ -d $a ]
then
ls -d $a
fi
done
#for循环是以空格、回车符作为分割符分割。
20.11-20.12 while循环
- 语法 while 条件; do ...; done
;案例1
[[email protected] shell]# cat while.sh
#!/bin/bash
while :
do
load=`w|head -1 |awk -F ‘load average: ‘ ‘{print $2}‘| cut -d . -f1`
if [ $load -gt 10 ]
then
top|mail -s "load is high:$load" [email protected]
fi
sleep 30
done
;案例2
[[email protected] shell]# cat while2.sh
#!/bin/bash
while :
do
read -p "Please input a number:" n
if [ -z "$n" ]
then
echo "You did not enter the number."
continue
fi
n1=`echo $n|sed ‘s/[0-9]//g‘`
if [ ! -z "$n1" ]
then
echo "You can only enter a pure number."
continue
fi
break
done
echo $n
20.13 break跳出循环
[[email protected] shell]# cat break.sh
#!/bin/bash
for i in `seq 1 5`
do
echo $i
if [ $i -eq 3 ]
then
break
fi
echo $i
done
echo aaaaaa
20.14 continue结束本次循环
[[email protected] shell]# cat continue.sh
#!/bin/bash
for i in `seq 1 5`
do
echo $i
if [ $i -eq 3 ]
then
continue
fi
echo $i
done
echo $i
20.15 exit退出整个脚本
[[email protected] shell]# cat exit.sh
#!/bin/bash
for i in `seq 1 5`
do
echo $i
if [ $i -eq 3 ]
then
exit
fi
echo $i
done
echo $i
以上是关于shell脚本 for循环break跳出循环continue结束本次循环的主要内容,如果未能解决你的问题,请参考以下文章
for循环,while循环,break跳出循环,continue结束本次循环,exit直接退出脚本
for循环 while循环 break跳出循环 continue结束本次循环 exit退出整
for循环 while循环break跳出循环continue结束本次循环exit退出整个脚本