【Shell】Shell腳本(for循環,while循環,break跳出循環,continue結束本次循環)


目錄

for循環

while循環

SHELL加法運算及I++

 

 

for循環

 

for:https://www.cnblogs.com/EasonJim/p/8315939.html

 

for i in {1..10}#10 這個替換成${NUM} 不起作用

 

語法:for 變量名 in 條件 ; do done;

案例一:

計算1-100所有數字的和。

腳本:

#!/bin/bash

sum=0

for i in `seq 1 100`

do

    sum=$[$sum+$i]

done

    echo $sum

結果:

[root@congji ~]# sh 1-100.sh 

5050

案例二:

列出/etc/sysconfig下所有子目錄,並且使用ls -d命令查看。

腳本:

#/bin/bash

cd /etc/sysconfig

for i in `ls /etc/sysconfig`

do

    if [ -d $i ]

       then

       ls -d $i

    fi

done

結果:

[root@congji ~]# sh syscon.sh

cbq

console

modules

network-scripts

         

for循環有一個值得注意的地方:

案例3:

我們創建幾個文件,用for循環來ls他們。

[root@congji shili]# ll 

總用量 0

-rw-r--r-- 1 root root 0 1月  16 21:16 1.txt

-rw-r--r-- 1 root root 0 1月  16 21:16 2.txt

-rw-r--r-- 1 root root 0 1月  16 21:17 3 4.txt

[root@congji shili]# for i in `ls ./`;do echo $i ;done

1.txt

2.txt

3

4.txt

所以寫腳本的時候要注意

 

while循環

語法 while條件;do...;done

案例1:寫一個腳本來監控系統負載,當系統負載大於10時,發郵箱警告。

腳本:

#/bin/bash

while :

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@congji shell]# sh -x while.sh 

+ :

++ w

++ head -1

++ awk -F 'load average: ' '{print $2}'

++ cut -d. -f1

+ load=0

+ '[' 0 -gt 10 ']'

+ sleep 30

案例二:

類似於之前寫過的for循環的腳本,輸入一個數字,如果不是數字返回一個字符串,如果輸入為空返回一個字符串,如果是數字返回。

在看腳本之前,我們需要知道continue和break的意思。

continue是繼續的意思,也就是當運行結果不滿足條件時,在從頭循環一遍。

break是跳出循環的意思。

腳本:

#/bin/bash

while :

do

    read -p "please input a number: " n

    if [ -z "$n" ]

    then

       echo "你需要輸入東西."

       continue

    fi

    n1=`echo $n|sed 's/[0-9]//g'`

    if [ ! -z "$n1" ]

    then

       echo "你只能輸入一個純數字."

       continue

    fi

    break

done

echo $n

運行結果:

[root@congji shell]# sh while2.sh 

please input a number: 1d

你只能輸入一個純數字.

please input a number: 

你需要輸入東西.

please input a number: 1

1

break

break在while循環中,我們提到了,這里來寫一個腳本,加深印象

如果輸入的數字小於等於3,則返回數字,如果輸入的數字大於3,則返回aaaa

腳本:

#/bin/bash

read -p "please input a number: " i

if [ $i -lt 3 ]

    then

    echo $i

       break

else

    echo aaaa

fi

運行結果:

[root@congji shell]# sh break.sh 

please input a number: 1

1

[root@congji shell]# sh break.sh 

please input a number: 5

aaaa

 

continue結束本次循環,而break是跳出循環,要分清楚

[root@congji shell]# cat continue.sh 

#/bin/bash

for i in `seq 1 5`

do 

    echo $i

if [ $i -eq 3 ]

        then

           continue

        fi

        echo $i

done

[root@congji shell]# sh continue.sh 

[root@congji shell]# cat break.sh 

#/bin/bash

for i in `seq 1 5`

do 

     echo $i

if [ $i == 3 ]

    then

       break

fi

    echo $i

done

[root@congji shell]# sh break.sh 

 

對比兩個腳本我們可以發現,break相當於跳出循環,結束。而continue相當於結束本次循環,開始新的循環,

#!/bin/bash
min=1
while true
do
    echo $min
    min=`expr $min + 2`
done
~       

轉自;https://blog.51cto.com/13407306/2070389

SHELL加法運算及I++

 

shell中不支持像普通c語言中的i++操作,默認都是字符串操作,但是通過以下幾種方式可以進行變量的自增加

1、linux 用let 表示算術表達式 如下:

     i=0 

     let i +=1  或者 let 'i+=1'

2、let也可以用 (())替代,這種用法常見於for循環中

      ((i++))

for 循環中用法:

((for i=0;i<2;i++))

do

..

done

3、linux 中也可以用expr 

      i=`expr $i + 1`;

 

 4、還可以用如下模式  
       i=$[$i+1];
       i=$(( $i + 1 ))

---------------------
 

注意 例  1+2得不到想要的結果,只會輸出1+2這個字符串


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM