1. 程式人生 > >for循環,while循環,break跳出循環,continue結束本次循環,exit直接退出腳本

for循環,while循環,break跳出循環,continue結束本次循環,exit直接退出腳本

href 變量名 average 變量 inpu code 支持 bash eas

for循環
  • 語法:for 變量名 in 條件; do …; done
  • 案列1,算出1到10的數字相加等於多少並打印出過程
    [root@akuilinux01 shell]# cat for1.sh 
    #!/bin/bash
    sum=0
    for i in `seq 1 10`
    do
    sum=$[$sum+$i]
    echo "$sum + $i"
    done
    echo $sum
    [root@akuilinux01 shell]# sh -x for1.sh 
    + sum=0
    ++ seq 1 10
    + for i in ‘`seq 1 10`‘
    + sum=1
    + echo ‘1 + 1‘
    1 + 1
    + for i in ‘`seq 1 10`‘
    + sum=3
    + echo ‘3 + 2‘
    3 + 2
    + for i in ‘`seq 1 10`‘
    + sum=6
    + echo ‘6 + 3‘
    6 + 3
    + for i in ‘`seq 1 10`‘
    + sum=10
    + echo ‘10 + 4‘
    10 + 4
    + for i in ‘`seq 1 10`‘
    + sum=15
    + echo ‘15 + 5‘
    15 + 5
    + for i in ‘`seq 1 10`‘
    + sum=21
    + echo ‘21 + 6‘
    21 + 6
    + for i in ‘`seq 1 10`‘
    + sum=28
    + echo ‘28 + 7‘
    28 + 7
    + for i in ‘`seq 1 10`‘
    + sum=36
    + echo ‘36 + 8‘
    36 + 8
    + for i in ‘`seq 1 10`‘
    + sum=45
    + echo ‘45 + 9‘
    45 + 9
    + for i in ‘`seq 1 10`‘
    + sum=55
    + echo ‘55 + 10‘
    55 + 10
    + echo 55
    55
  • 文件列表循環,列出/root/shell/目錄下子目錄中的所有文件
    [root@akuilinux01 shell]# cat for2.sh 
    #!/bin/bash
    cd /root/shell/
    for a in `ls /root/shell`
    do
    if [ -d $a ]
    then 
      ls $a
    fi
    done
    [root@akuilinux01 shell]# sh -x for2.sh 
    + cd /root/shell/
    ++ ls /root/shell
    + for a in ‘`ls /root/shell`‘
    + ‘[‘ -d 1 ‘]‘
    + ls 1
    1.txt  2.txt  3.txt
    + for a in ‘`ls /root/shell`‘
    + ‘[‘ -d 1.sh ‘]‘
    + for a in ‘`ls /root/shell`‘
    + ‘[‘ -d 2 ‘]‘
    + ls 2
    a.txt  b.txt  c.txt
    + for a in ‘`ls /root/shell`‘
    + ‘[‘ -d 3 ‘]‘
    + ls 3
    + for a in ‘`ls /root/shell`‘
    + ‘[‘ -d 4 ‘]‘
    + ls 4
    + for a in ‘`ls /root/shell`‘
    + ‘[‘ -d case.sh ‘]‘
    + for a in ‘`ls /root/shell`‘
    + ‘[‘ -d file1.sh ‘]‘
    + for a in ‘`ls /root/shell`‘
    + ‘[‘ -d file2.sh ‘]‘
    + for a in ‘`ls /root/shell`‘
    + ‘[‘ -d for1.sh ‘]‘
    + for a in ‘`ls /root/shell`‘
    + ‘[‘ -d for2.sh ‘]‘
    + for a in ‘`ls /root/shell`‘
    + ‘[‘ -d if1.sh ‘]‘
    + for a in ‘`ls /root/shell`‘
    + ‘[‘ -d if2.sh ‘]‘
    + for a in ‘`ls /root/shell`‘
    + ‘[‘ -d if3.sh ‘]‘
    + for a in ‘`ls /root/shell`‘
    + ‘[‘ -d if4.sh ‘]‘
    [root@akuilinux01 shell]# sh for2.sh 
    1.txt  2.txt  3.txt
    a.txt  b.txt  c.txt
  • for循環是以空格和回車作為分割符,所以不支持列出帶有空格的文件名的文件

    while循環

  • 語法 while 條件; do … ; done
  • 案列:當系統的負載大於10的時候就發一封郵件
    [root@akuilinux01 shell]# cat while1.sh 
    #!/bin/bash
    #死循環,:是一個死循環,也可以寫成1或者true
    while : 
    do
    #截取負載的值並賦給變量load
    load=`w|head -1|awk -F ‘load average: ‘ ‘{print $2}‘|cut -d. -f1`
    #如果負載大於10就報警並發送郵件,設置30秒查一次  
    if [ $load -gt 10 ]
    then
     /usr/lib/zabbix/alertscripts/mail.py [email protected] "load is high:$load"
    fi
    sleep 30
    done
    [root@akuilinux01 shell]# sh -x while1.sh 
    + :
    ++ head -1
    ++ cut -d. -f1
    ++ w
    ++ awk -F ‘load average: ‘ ‘{print $2}‘
    + load=0
    + ‘[‘ 0 -gt 10 ‘]‘
    + sleep 30
    ^C
  • 截取負載的值
    [root@akuilinux01 shell]# uptime|awk -F ‘load average: ‘ ‘{print $2}‘
    0.01, 0.03, 0.05
    [root@akuilinux01 shell]# uptime|awk -F ‘load average: ‘ ‘{print $2}‘|cut -d. -f1
    0
    #w|head -1也可以寫成uptime
    [root@akuilinux01 shell]# w |head -1
    01:02:36 up 58 min,  1 user,  load average: 0.07, 0.04, 0.05
    [root@akuilinux01 shell]# uptime
    01:02:46 up 59 min,  1 user,  load average: 0.06, 0.04, 0.05
  • 案列2:數字輸入交互
    [root@akuilinux01 shell]# cat while2.sh 
    #!/bin/bash
    while :
    do
    read -p "Please input a number: " n
    #如果你什麽也沒輸入
    if [ -z "$n" ]
    then
        echo "你需要輸入一個數字"
    #從頭開始循環
        continue
    fi
    #過濾數字並賦予n1
    n1=`echo $n|sed ‘s/[0-9]//g‘`
    #如果n1不為空,提示
    if [ -n "$n1" ]
    then
        echo "你只能輸入一個純數字"
        continue
    fi
    #退出循環
    break
    done
    echo $n
    [root@akuilinux01 shell]# sh while2.sh 
    Please input a number: 
    你需要輸入一個數字
    Please input a number: a
    你只能輸入一個純數字
    Please input a number: 2
    2

    break跳出循環

  • 案列:當i等於3時,跳出循環並打印aaaa
    [root@akuilinux01 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 aaaa
    [root@akuilinux01 shell]# sh -x break.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 aaaa
    aaaa

    continue結束本次循環

  • 忽略continue之下的代碼,直接進行下一次循環
  • 案列:當i不等於3的時候打印出i,當i等於3的時候退出本次循環不打印
    [root@akuilinux01 shell]# cat continue.sh 
    #!/bin/bash
    for i in `seq 1 5`
    do
    if [ $i -eq 3 ]
     then
         continue
    fi
    echo $i
    done
    echo aaaa
    [root@akuilinux01 shell]# sh -x continue.sh 
    ++ seq 1 5
    + for i in ‘`seq 1 5`‘
    + ‘[‘ 1 -eq 3 ‘]‘
    + echo 1
    1
    + for i in ‘`seq 1 5`‘
    + ‘[‘ 2 -eq 3 ‘]‘
    + echo 2
    2
    + for i in ‘`seq 1 5`‘
    + ‘[‘ 3 -eq 3 ‘]‘
    + continue
    + for i in ‘`seq 1 5`‘
    + ‘[‘ 4 -eq 3 ‘]‘
    + echo 4
    4
    + for i in ‘`seq 1 5`‘
    + ‘[‘ 5 -eq 3 ‘]‘
    + echo 5
    5
    + echo aaaa
    aaaa

    exit直接退出腳本

  • 案列:當i不等於3的時候打印出i,當i等於3的時候直接退出腳本
    [root@akuilinux01 shell]# cat exit.sh 
    #!/bin/bash
    for i in `seq 1 5`
    do
    if [ $i -eq 3 ]
     then
         exit
    fi
    echo $i
    done
    echo aaaa
    [root@akuilinux01 shell]# sh -x exit.sh 
    ++ seq 1 5
    + for i in ‘`seq 1 5`‘
    + ‘[‘ 1 -eq 3 ‘]‘
    + echo 1
    1
    + for i in ‘`seq 1 5`‘
    + ‘[‘ 2 -eq 3 ‘]‘
    + echo 2
    2
    + for i in ‘`seq 1 5`‘
    + ‘[‘ 3 -eq 3 ‘]‘
    + exit
    [root@akuilinux01 shell]# sh exit.sh 
    1
    2

    擴展

  • select用法

for循環,while循環,break跳出循環,continue結束本次循環,exit直接退出腳本