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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了for循环while循环break循环continue结束本次循环exit退出整个脚本相关的知识,希望对你有一定的参考价值。

for循环

  • for循环案例1
#打印1到100
[[email protected] shell]# vi for1.sh
[[email protected] shell]# cat for1.sh 
#!/bin/bash
for i in `seq 1 100 `
do 
   echo $i
done
[[email protected] shell]# sh for1.sh 
1
2
3
4
5
6
7
8
9
10
11
...

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

#每次循环相加的值
[[email protected] shell]# vi for1.sh
[[email protected] 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
[[email protected] shell]# sh for1.sh 
0 + 1
1
1 + 2
3
3 + 3
6
....
4950 + 100
5050
  • for循环案例2
[[email protected] shell]# vi for2.sh
[[email protected] shell]# cat for2.sh 
#!/bin/bash
cd /etc/
for a in ls /etc/
do
    if [ -d $a ]
    then
        echo $a
        ls $a
    fi
done
[[email protected] 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的时候就发一封邮件告警。
脚本示例:

[[email protected] aming]# vi while1.sh
[[email protected] 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 [email protected] "load high" "$load"
    fi
    sleep 30
done
[[email protected] 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
[[email protected] aming]# 

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

[[email protected] shell]# vim while2.sh
[[email protected] 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
[[email protected] 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
[[email protected] shell]# vim while2.sh
[[email protected] 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
[[email protected] 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跳出循环

  • 把整个循环退出
[[email protected] aming]# vi break1.sh 
[[email protected] 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
[[email protected] aming]# sh break1.sh 
1
1
2
2
3
aaaaa
[[email protected] 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
[[email protected] aming]# 

continue结束本次循环

  • 忽略continue之下的代码,直接进行下一次循环
[[email protected] aming]# vi continue.sh
[[email protected] 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
[[email protected] aming]# sh continue.sh 
1
1
2
2
3
4
4
5
5
aaaaa
[[email protected] 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
[[email protected] aming]# 

exit退出整个脚本

  • 直接退出整个脚本,脚本示例
[[email protected] aming]# vi exit.sh
[[email protected] 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
[[email protected] aming]# sh exit.sh 
1
1
2
2
3
[[email protected] 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
[[email protected] aming]# 

以上是关于for循环while循环break循环continue结束本次循环exit退出整个脚本的主要内容,如果未能解决你的问题,请参考以下文章

shell break与continue

for与while循环break跳出循环continue结束本次循环exit退出脚本

C;如果我在 while 循环内的 for 循环中放置一个 break

六十九for循环while循环break跳出循环continue结束本次循环exit退出整

python之while与for循环,break与continue

一文了解Python中的循环(for while break continue 嵌套循环...)