您的位置:首页 > 产品设计 > UI/UE

for循环、while循环、break循环、continue结束本次循环、exit退出整个脚本

2018-02-07 17:51 956 查看

for循环

for循环案例1

#打印1到100
[root@garytao-01 shell]# vi for1.sh
[root@garytao-01 shell]# cat for1.sh
#!/bin/bash
for i in `seq 1 100 `
do
echo $i
done
[root@garytao-01 shell]# sh for1.sh
1
2
3
4
5
6
7
8
9
10
11
...

#打印数字叠加的值
[root@garytao-01 shell]# vi for1.sh
[root@garytao-01 shell]# cat for1.sh
#!/bin/bash
sum=0
for i in `seq 1 100 `
do
sum=$[$sum+$i]  #核心语句
done
echo $sum
[root@garytao-01 shell]# sh for1.sh
5050

#每次循环相加的值
[root@garytao-01 shell]# vi for1.sh
[root@garytao-01 shell]# cat for1.sh
#!/bin/bash
sum=0
for i in `seq 1 100`
do
echo "$sum + $i"
sum=$[$sum+$i]
echo $sum
done
echo $sum
[root@garytao-01 shell]# sh for1.sh
0 + 1
1
1 + 2
3
3 + 3
6
....
4950 + 100
5050


for循环案例2

[root@garytao-01 shell]# vi for2.sh
[root@garytao-01 shell]# cat for2.sh
#!/bin/bash
cd /etc/
for a in ls /etc/
do
if [ -d $a ]
then
echo $a
ls $a
fi
done
[root@garytao-01 shell]# sh for2.sh
/etc/
adjtime          exports.d       ld.so.conf.d          pki       shadow-
aliases          favicon.png         lftp.conf             plymouth      shells
aliases.db       filesystems         libaudit.conf         pm        skel
alternatives         firewalld       libnl             polkit-1      ssh
anacrontab       fonts           libuser.conf          popt.d        ssl
asound.conf      fstab           locale.conf           postfix       statetab
audisp           fuse.conf       localtime             ppp       statetab.d
audit            gcrypt          login.defs            prelink.conf.d    subgid
bash_completion.d    GeoIP.conf      logrotate.conf        printcap      subuid


for循环会把命令空格及回车当作是一个分隔符,这个在写脚本的时候需要注意,如下示例



while循环

语法 while 条件; do … ; done

需求:每隔半分钟检查系统负载,当系统负载大于10的时候就发一封邮件告警。
脚本示例:

[root@garytao-01 aming]# vi while1.sh
[root@garytao-01 aming]# cat while1.sh
#!/bin/bash
while true
do
load=`w|head -1|awk -F 'load average: ' '{print $2}'|cut -d. -f1`
if [ $load -gt 10 ]
then
/usr/local/sbin/mail.py xxx@qq.com "load high" "$load"
fi
sleep 30
done
[root@garytao-01 aming]# sh -x while1.sh
+ true
++ w
++ head -1
++ awk -F 'load average: ' '{print $2}'
++ cut -d. -f1
+ load=0
+ '[' 0 -gt 10 ']'
+ sleep 30
+ true
++ w
++ head -1
++ awk -F 'load average: ' '{print $2}'
++ cut -d. -f1
+ load=0
+ '[' 0 -gt 10 ']'
+ sleep 30
^C
[root@garytao-01 aming]#

需求:在循环过程中需要人为的输入一个数字
脚本示例:

[root@garytao-01 shell]# vim while2.sh
[root@garytao-01 shell]# cat while2.sh
#!/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 [ ! -z "$n1" ]
then
echo "you just only input numbers."
continue
fi
break
done
echo $n
[root@garytao-01 shell]# sh -x while2.sh
+ :
+ read -p 'Please input a number: ' n
Please input a number:
+ '[' -z '' ']'
+ echo 'you need input sth.'
you need input sth.
+ continue
+ :
+ read -p 'Please input a number: ' n
Please input a number: 23
+ '[' -z 23 ']'
++ echo 23
++ sed 's/[0-9]//g'
+ n1=
+ '[' '!' -z '' ']'
+ break
+ echo 23
23
[root@garytao-01 shell]# vim while2.sh
[root@garytao-01 shell]# cat while2.sh
#!/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
[root@garytao-01 shell]# sh -x while2.sh
+ :
+ read -p 'Please input a number: ' n
Please input a number:
+ '[' -z '' ']'
+ echo 'you need input sth.'
you need input sth.
+ continue
+ :
+ read -p 'Please input a number: ' n
Please input a number: 32kgg
+ '[' -z 32kgg ']'
++ echo 32kgg
++ sed 's/[0-9]//g'
+ n1=kgg
+ '[' -n kgg ']'
+ echo 'you just only input numbers.'
you just only input numbers.
+ continue
+ :
+ read -p 'Please input a number: ' n
Please input a number: 45
+ '[' -z 45 ']'
++ echo 45
++ sed 's/[0-9]//g'
+ n1=
+ '[' -n '' ']'
+ break
+ echo 45
45

break跳出循环

把整个循环退出

[root@garytao-01 aming]# vi break1.sh
[root@garytao-01 aming]# cat break1.sh
#!/bin/bash
for i in `seq 1 5`
do
echo $i
if [ $i -eq 3 ]
then
break
fi
echo $i
done
echo aaaaa
[root@garytao-01 aming]# sh break1.sh
1
1
2
2
3
aaaaa
[root@garytao-01 aming]# sh -x break1.sh
++ seq 1 5
+ for i in '`seq 1 5`'
+ echo 1
1
+ '[' 1 -eq 3 ']'
+ echo 1
1
+ for i in '`seq 1 5`'
+ echo 2
2
+ '[' 2 -eq 3 ']'
+ echo 2
2
+ for i in '`seq 1 5`'
+ echo 3
3
+ '[' 3 -eq 3 ']'
+ break
+ echo aaaaa
aaaaa
[root@garytao-01 aming]#

continue结束本次循环

忽略continue之下的代码,直接进行下一次循环

[root@garytao-01 aming]# vi continue.sh
[root@garytao-01 aming]# 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 aaaaa
[root@garytao-01 aming]# sh continue.sh
1
1
2
2
3
4
4
5
5
aaaaa
[root@garytao-01 aming]# sh -x continue.sh
++ seq 1 5
+ for i in '`seq 1 5`'
+ echo 1
1
+ '[' 1 -eq 3 ']'
+ echo 1
1
+ for i in '`seq 1 5`'
+ echo 2
2
+ '[' 2 -eq 3 ']'
+ echo 2
2
+ for i in '`seq 1 5`'
+ echo 3
3
+ '[' 3 -eq 3 ']'
+ continue
+ for i in '`seq 1 5`'
+ echo 4
4
+ '[' 4 -eq 3 ']'
+ echo 4
4
+ for i in '`seq 1 5`'
+ echo 5
5
+ '[' 5 -eq 3 ']'
+ echo 5
5
+ echo aaaaa
aaaaa
[root@garytao-01 aming]#

exit退出整个脚本

直接退出整个脚本,脚本示例

[root@garytao-01 aming]# vi exit.sh
[root@garytao-01 aming]# 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 aaaaa
[root@garytao-01 aming]# sh exit.sh
1
1
2
2
3
[root@garytao-01 aming]# sh -x exit.sh
++ seq 1 5
+ for i in '`seq 1 5`'
+ echo 1
1
+ '[' 1 -eq 3 ']'
+ echo 1
1
+ for i in '`seq 1 5`'
+ echo 2
2
+ '[' 2 -eq 3 ']'
+ echo 2
2
+ for i in '`seq 1 5`'
+ echo 3
3
+ '[' 3 -eq 3 ']'
+ exit
[root@garytao-01 aming]#
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  for 循环 while
相关文章推荐