shell 編程for循環總結

shell編程for循環總結


   在shell編程中,循環的執行是將某代碼段重複運行多次,常用循環有for、while和until循環,其中for循環經常用於有限次循環,for循環的語法結構有如下兩種:

   第一種:shell傳統for循環語法結構

for 變量名 in 變量取值列表;do

指令…

done

其中取值列表通常又有5種方式:

(1)直接給出列表,列表中間用空格隔開

[root@centos7 ~]#for i in 1 3 5;do echo $i;done

1

3

5

(2)整數列表

  (a) {start..end}

[root@centos7 ~]#for i in {1..5..2};do echo $i;done

1

3

5

  (b)$(seq start  step  end)

[root@centos7 ~]#for i in $(seq 1 2 5);do echo $i;done

1

3

5

(3)返回命令列表

[root@centos7 ~]#mkdir test

[root@centos7 ~]#cd test/

[root@centos7 ~/test]#touch 1.sh

[root@centos7 ~/test]#touch 3.sh

[root@centos7 ~/test]#touch 5.sh

[root@centos7 ~/test]#for i in $(ls);do echo $i;done

1.sh

3.sh

5.sh

(4)使用glob,如:*.sh

[root@centos7  ~/test]#for i in *\.sh; do echo $i;done   #選擇當前目錄下滿足*.sh

1.sh

3.sh

5.sh

(5)使用變量引用如$@,$*

[root@centos7  ~/test]#vim 1

[root@centos7  ~/test]#chmod +x 1

1 #!/bin/bash

  2 for i in $@;do

  3    rm -rf $i

  4 done

[root@centos7  ~/test]#./1 1.sh 3.sh 5.sh

[root@centos7  ~/test]#ls

1                                                #驗證1.sh 3.sh 5.sh已經刪除

第二種結構體:C語言型結構體

for ((exp1; exp2; exp3))

do

   指令

done

[root@centos7  ~/test]#for ((i=1; i<=5; i=i+2));do echo $i;done

1    #注意:a=a++ 和 a=a+2的區別,a=a+2是表達式,=號的優先級較低,因此先+後賦值

3

5

   基礎案例分析:

 1.打印99乘方口訣,

[root@centos7 ~/scripts]#vim sufakoujue.sh
#!/bin/bash
for i in $(seq 9);do
 
    for j in $(seq $i);do
 
     let sum=$i*$j
 
     echo -n "$j*$i=$sum "
 
    done
 
   echo                          #打印換行,每循環一次進行一次換行
done
[root@centos7 ~/scripts]#chmod +x sufakoujue.sh
[root@centos7 ~/scripts]#./suanfakoujue.sh
1*1=1
1*2=2 2*2=4
1*3=3 2*3=6 3*3=9
1*4=4 2*4=8 3*4=12 4*4=16
1*5=5 2*5=10 3*5=15 4*5=
20 5*5=25
1*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36
1*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49
1*8=8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64
1*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81

2.判斷當前目錄下所有文件的類型

[root@centos7~/test]#vim panduan.sh 
#!/bin/bash
 for filename in *;do    #利用for語句列表中第四種方式使用通配符glob
   if[ -f $filename ];then
    echo $filename is file
  else [ -d $filename ]
    echo$filename is directory
   fi
 done
[root@centos7 ~/test]#chmod+x panduan.sh
[root@centos7 ~/test]#./panduan.sh
1 is file
1.sh is file
2 is file
3 is directory
3.sh is file
5 is directory
5.sh is file
panduan.sh is file

3.計算1+2+3+4+...+n之和,其中n由用戶自己輸入

[root@centos7~/test]#vim sum.sh
#!/bin/bash
 read-p "please inputyour inter: " inter
 #+++++++++++++Makesure the parameter is not empty++++++++
 [-z $inter ] && echo "you must input a inter!" && exit1
 #+++++++++++++Makesure the parameter is integer++++++++++
 if[[ $inter =~ ^[0-9]+$  ]];then
    for i in $(seq $inter);do
      let sum=$sum+i
    done
    echo"1+2+..+$i=$sum"
  else
    echo"you must input a inter!"
 fi  
[root@centos7 ~/test]#chmod+x sum.sh
[root@centos7 ~/test]#./sum.sh
please input your inter: 4
1+2+..+4=10

4.計算100之內能被3整除的整數之和

[root@centos7~/test]#vim 3sum.sh
#!/bin/bash
for i in $(seq 3 3 100);do       #利用整數列表步進的方式
  let sum=$sum+i
done
echo "3+6+...99=$sum"
[root@centos7 ~/test]#chmod +x 3sum.sh
[root@centos7 ~/test]#./3sum.sh
3+6+...99=1683

5.判斷局域網192.168.1.0的網段主機存活狀態

[root@centos7 ~/test]#vim hostping.sh
net=192.168.1
  for i in {1..255};do
   {
  if ping -c1 -w1 $net.$i &>/dev/null;then
   echo "$net.$i is exsit"
  fi
  }&             #通過放入後臺並行執行提高執行效率,避免順序執行
 done
 wait             #避免敲回車退出腳本,可以通過help wait查看幫助
[root@centos7 ~/test]#chmod +x hostping.sh
[root@centos7 ~/test]#./hostping.sh
192.168.1.48 is exsit

6.打印等腰三角形

[root@centos7 ~/test]vim dengyao.sh

 read -p "please input a inter: " a

   for i in $(seq$a);do

      let j=$a-$i+1

      let k=2*$i-1

  #++++++++打印每行等腰三角的空白字符+++++   

      for l in $(seq $j);do

            echo -n " "            

      done

  #++++++++打印等腰三角形的構成圖形+++++++

      for n in $(seq $k);do

            echo -n ""

      done

  #++++++++每一行結束後進行換行+++++++++++

      echo               

 done

[root@centos7 ~/test]./dengyao.sh

********具體詳情請諮詢微信:QQ767743577  郵箱地址: [email protected],有問必答,有答必應,人人爲我,我爲人人*******
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章