1. 程式人生 > >shell腳本編程學習筆記-for循環

shell腳本編程學習筆記-for循環

linux shell

1.for循環結構

1.1 for循環結構語法

語法:

for 變量名 in 變量取值列表

do

指令…

done

提示:在此結構中“in 變量取值列表”可省略,省略時相當於in “$@”,使用for i 就相當於for i in “$@”。

1.2 C語言型for循環結構

語法:

for((exp1;exp2;exp3))

do

指令…

Done

例:while循環和for循環的對比

[root@shellbiancheng jiaobenlianxi]# cat while4.sh 
#!/bin/bash
i=1
while((i<=10))
do
    echo $i
    ((i++))
done
[root@shellbiancheng jiaobenlianxi]# cat for1.sh 
#!/bin/bash

for((i=1;i<=10;i++))
do
    echo $i

done

說明:

(1)程序持續運行多用while,包括守護進程還有配合read讀入循環處理。

(2)有限次循環多用for,工作中for使用更多。

學習記憶的方法:

for 男人 in 世界

do

if [ 有房 ] && [ 有車 ] && [ 存款 ] && [ 會做家務 ] && [ 帥氣 ] && [ 體貼 ] && [ 逛街買東西 ];then

echo “我喜歡你”

else

rm –f 男人

fi

done

1.3 for循環基礎例子

1.3.1 範例1:直接列出變量列表所有元素,打印1,2,3,4,5

方法1:

[root@shellbiancheng jiaobenlianxi]#  cat for3.sh 
for n in 1 2 3 4 5 

do
echo $n
done

方法2:使用大括號的方法

[root@shellbiancheng jiaobenlianxi]# cat for4.sh 
for n in {1..10}

do
echo 192.168.1.$n
done

方法3:使用seq

[root@shellbiancheng jiaobenlianxi]# cat for5.sh 
for n in  `seq 5`
do
echo 192.168.1.$n
done

用for循環打印當前目錄的文件名,用seq實現

提示:變量取值列表也可以跟命令的輸出

[root@shellbiancheng jiaobenlianxi]# cat for5.sh 
for n in  `ls`
do
echo $n
done

1.3.2 範例2:隨機數生成文件

批量生成文件,文件名隨機md5處理後,選八位。

[root@shellbiancheng jiaobenlianxi]# cat suijiwnjian.sh 
for((i=1;i<=10;i++))
do
    mkdir -p ./test
    touch ./test/` echo $RANDOM|md5sum|cut -c 1-8`_finished.html
done
[root@shellbiancheng jiaobenlianxi]# ls test/
0ac2453a_finished.html  8b90c52b_finished.html  c4eb22c9_finished.html
2c1f5e87_finished.html  a22717d3_finished.html  db258218_finished.html
6d6bc69f_finished.html  a4aca3b0_finished.html
6d7c516a_finished.html  c21f3d55_finished.html

1.3.3 範例3:批量修改文件名

將上面生成的10以.html結尾的文件修改成以.jpg結尾。並且把finished去掉,只用for循環。

方法一:

[root@shellbiancheng jiaobenlianxi]# cat piliangxiugai1.sh 
for f in `ls test/*.html`
do
    mv $f `echo $f|sed ‘s#_finished.html#.jpg#g‘`
done
[root@shellbiancheng jiaobenlianxi]# ls test/
0ac2453a.jpg  6d6bc69f.jpg  8b90c52b.jpg  a4aca3b0.jpg  c4eb22c9.jpg
2c1f5e87.jpg  6d7c516a.jpg  a22717d3.jpg  c21f3d55.jpg  db258218.jpg

方法二:for循環加變量部分截取的方法

[root@shellbiancheng jiaobenlianxi]# cat piliangxiugai3.sh 
for file in `ls test/*.html`
do
    /bin/mv $file "${file%_finished*}.jpg"
done

方法三:ls結合awk實現

[root@shellbiancheng test]# ls
0513d6f0_finished.html  6ab3573c_finished.html  8abfa660_finished.html
1a9335f3_finished.html  70018e1c_finished.html  bb6763ab_finished.html
559d16bc_finished.html  74e206b5_finished.html
58fc15d7_finished.html  7878f84a_finished.html
[root@shellbiancheng test]# ls |awk -F ‘[_]‘ ‘{print "mv " $0,$1".jpg"}‘|bash

[root@shellbiancheng test]# ls
0513d6f0.jpg  559d16bc.jpg  6ab3573c.jpg  74e206b5.jpg  8abfa660.jpg
1a9335f3.jpg  58fc15d7.jpg  70018e1c.jpg  7878f84a.jpg  bb6763ab.jpg

方法四:通過rename實現

[root@shellbiancheng test]# ls
16d72ca3_finished.html  673c62da_finished.html  b9328787_finished.html
37690b31_finished.html  72ebc17d_finished.html  e37aeed5_finished.html
37e66161_finished.html  a5050e54_finished.html
4efe1f4c_finished.html  b2553039_finished.html
[root@shellbiancheng test]# rename "_finished.html" ".jpg" *
[root@shellbiancheng test]# ls
16d72ca3.jpg  37e66161.jpg  673c62da.jpg  a5050e54.jpg  b9328787.jpg
37690b31.jpg  4efe1f4c.jpg  72ebc17d.jpg  b2553039.jpg  e37aeed5.jpg

1.3.4 範例4:

實戰案例:開發腳本實現僅設置sshd crond rsyslog network 開機自啟動

[root@shellbiancheng jiaobenlianxi]# cat chkconfigoff.sh 
LANG=en
chkconfig --list|grep 3:on|awk ‘{print $1}‘ >b.log
for name in `chkconfig --list|grep 3:on|awk ‘{print $1}‘|egrep -v "sshd|crond|rsyslog|network"`

do
  chkconfig $name off

done
[root@shellbiancheng jiaobenlianxi]# cat chkconfigon.sh 
LANG=en
for name in `cat b.log`
do
    chkconfig $name on
done

1.3.5 範例5:用for循環實現1-100之和

方法一:

[root@shellbiancheng jiaobenlianxi]# cat for1-100.sh 
for((i=1;i<=100;i++))
do
    let sum+=i
done
echo $sum
[root@shellbiancheng jiaobenlianxi]# sh for1-100.sh 
5050

方法二:

[root@shellbiancheng jiaobenlianxi]# cat for1-1000.sh 
for n in `seq 100`
do
    ((sum+=n))
done
echo $sum

方法三:

[root@shellbiancheng jiaobenlianxi]# cat for1-100_3.sh 
for n in `seq 100`
do
   a="$(($n*($n+1)/2))"
done
echo $a

shell腳本編程學習筆記-for循環