1. 程式人生 > >for循環、while循環、break循環、continue結束本次循環、exit退出整個腳本

for循環、while循環、break循環、continue結束本次循環、exit退出整個腳本

核心 一次 src 郵件告警 aso 分隔符 fuse crypt 語句

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 [email protected] "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循環、break循環、continue結束本次循環、exit退出整個腳本