for循环、while循环、break跳出循环、continue结束本次循环、exit退出整个脚本
2017-11-28 12:17
1096 查看
for循环
1、求1加到100的数字的合是多少sum=0 for i in `seq 1 100`;do sum=$[sum+$i];echo "$sum";done
2、循环目录列表
[root@centos7 shell]# vi for.sh
#!/bin/bash
dir=/usr/local/sbin/
for a in
ls $dir
do
if [ -d $a ]
then
echo $a
ls $a
fi
done
echo "No directory file!"
while循环
格式: while 条件;do…;done1、当系统负载大于10的时候,发送邮件,每隔30秒执行一次。
while : 表示真命题,相当于while true
#!/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" abc@111.com
fi
sleep 30
done
2、
#!/bin/bash
while :
do
read -p 'please input a num:' n
if [ -z "$n" ];then
echo "please input num"
continue
fi
echo $n
n1=
echo $n|sed 's#[0-9]##g'
if [ -n "$n1" ];then
echo "please input num"
exit 2
else
echo $n
break
fi
done
#continue:中断本次while循环后重新开始;
#break:表示跳出本层循环,即该while循环结束
break跳出循环
1、当等于3就跳出循环#!/bin/bash
for i in
seq 1 5
do
echo "$i"
if [ $i -eq 3 ]
then
break
fi
echo "$i"
done
echo "Finished!"
结果:
[root@centos7 shell]# sh break.sh
1
1
2
2
3
Finished!
continue结束本次循环
1、当等于3了就跳过继续执行,本次循环结束,开始下一个循环#!/bin/bash
for i in
seq 1 5
do
echo "$i"
if [ $i -eq 3 ]
then
continue
fi
echo "$i"
done
echo "Finished!"
结果:
1
1
2
2
3
4
4
5
5
Finished!
exit退出整个脚本
1、当等于3就退出整个脚本#!/bin/bash
for i in
seq 1 5
do
echo "$i"
if [ $i -eq 3 ]
then
exit
fi
echo "$i"
done
select 选择器
1、#!/bin/bash
echo "please input 1.w 2.top 3.free 4.quit"
select com in w top free quit
do
case $com in
w)
w
;;
top)
top
;;
free)
free
;;
*)
exit
;;
esac
done
执行结果:
[root@centos7 shell]# sh select.sh
please input 1.w 2.top 3.free 4.quit
1) w
2) top
3) free
4) quit
相关文章推荐
- for循环、 while循环、break跳出循环、continue结束本次循环、exit退出整个脚本
- for循环、while循环、break跳出循环、continue结束本次循环、exit退出整个脚本
- 20.10 for循环 20.11/20.12 while循环 20.13 break跳出循环 20.14 continue结束本次循环 20.15 exit退出整个脚本
- for循环,while循环,break跳出循环,continue结束本次循环,exit退出整个脚本
- 【shell编程】for循环、while循环、break跳出循环、continue结束本次循环和exit退出整个脚本
- for循环、while循环、break循环、continue结束本次循环、exit退出整个脚本
- for循环 while循环 break跳出循环 continue结束本次循环 exit退出整个脚本
- for、while循环、break跳出循环、continue结束本次循环、exit退出脚本
- 十六周四次课 2018.02.07 for循环、while循环、break跳出循环、continue结束本次循环、exit退出整
- for循环 while循环 break跳出循环 continue结束本次循环 exit退出整个脚本
- 【CentOS 7Shell编程7】,break跳出本次循环和continue结束本次循环以及exit直接退出脚本#180212
- Shell脚本(for循环,while循环,break跳出循环,continue结束本次循环)
- linux学习第六十五篇:for循环,while循环, break跳出循环,continue结束本次循环
- shell脚本 for循环、break跳出循环、continue结束本次循环
- continue结束本次循环和break跳出当前循环
- for循环、while循环、continue、break、exit解析、select用法
- break 退出循环 continue 提前进入下一轮循环 while的特殊用法
- 【php】PHP中跳出循环的方法及continue、break、exit的用法详解
- php中的break,continue,return,exit,die等跳出或终止循环笔记
- 解析PHP跳出循环的方法以及continue、break、exit的区别介绍