for命令
bash shell提供了for命令,允許你建立一個遍歷一系列的迴圈。
for var in list do commands done
1、讀取列表中的值
for命令最基本的用法就是遍歷for命令自身所定義的一系列值。
[root@node1 ljy]# more ceshi.sh #!/bin/bash for test in football basketball volleyball do echo "you like $test" done [root@node1 ljy]# sh ceshi.sh you like football you like basketball you like volleyball
2、讀取列表中的複雜值
如果列表中數值比較麻煩,for命令可能會識別異常。
[root@node1 ljy]# more ceshi.sh #!/bin/bash for test in I don\'t know "new york" do echo "word:$test" done [root@node1 ljy]# sh ceshi.sh word:I word:don't word:know word:new york
可以使用轉義字元(\)或者雙引號來定義用到單引號的值。
for迴圈假定每個值都是空格分割,如果要包含空格的數值,可以用""雙引號。
3、從變數讀取列表
可以將一些列的值儲存在變數中,然後遍歷變數中的整個列表。
[root@node1 ljy]# more ceshi.sh #!/bin/bash list="baketball football volleyball" for test in $list do echo "word:$test" done [root@node1 ljy]# sh ceshi.sh word:baketball word:football word:volleyball
4、從命令讀取值
[root@node1 ljy]# more one.txt football basketball volleyball [root@node1 ljy]# more ceshi.sh #!/bin/bash file=/ljy/one.txt for test in $(cat $file) do echo "word:$test" done [root@node1 ljy]# sh ceshi.sh word:football word:basketball word:volleyball
5、更改欄位分隔符
IFS(內部欄位分隔符)環境變數定義了bash shell用做欄位分隔符的一些列字元,預設情況下,bash shell將以下字元當做欄位分隔符:
1、空格
2、製表符
3、換行符
IFS=$'\n'這個語句表示bash shell會在資料中忽視空格和製表符。
[root@node1 ljy]# more one.txt football basketball volleyball new york [root@node1 ljy]# more ceshi.sh #!/bin/bash IFS=$'\n' file=/ljy/one.txt for test in $(cat $file) do echo "word:$test" done [root@node1 ljy]# sh ceshi.sh word:football word:basketball word:volleyball word:new york
假如你遍歷一個檔案用冒號分隔的值,可以設定為IFS=:
6、萬用字元讀取目錄
可以用for命令來遍歷目錄中的檔案,進行操作時必須在檔名或路徑中加入萬用字元*。他會強制shell使用檔案擴充套件匹配。
[root@node1 ljy]# more ceshi.sh #!/bin/bash file=/ljy/* for file in /home/* do if [ -d "$file" ] then echo "$file is a directory" elif [ -f "$file" ] then echo "$file is a file" fi done [root@node1 ljy]# sh ceshi.sh /home/123 is a file /home/lisi is a file /home/ljy is a directory /home/test.sh is a file /home/zhangsan is a directory
在Linux中允許目錄或者檔名中包含空格,要適應這種情況,應該講$file變數用雙引號圈起來,如果不這麼做可能包含空格的目錄會有問題。
while命令
while明亮的基本格式:
while test command do other commands done
只要while測試條件成立,while命令就會不停的迴圈執行下去
例項:
[root@node1 ljy]# more ceshi.sh #!/bin/bash var=10 while [ $var -gt 2 ] do echo "$var" var=$[ $var - 1 ] done [root@node1 ljy]# sh ceshi.sh 10 9 8 7 6 5 4 3
until命令
一旦測試命令成立迴圈結束。
[root@node1 ljy]# more ceshi.sh #!/bin/bash var=10 until [ $var -eq 2 ] do echo "$var" var=$[ $var - 1 ] done [root@node1 ljy]# sh ceshi.sh 10 9 8 7 6 5 4 3
巢狀迴圈
迴圈語句可以在迴圈內使用任意型別的命令,包括其他迴圈命令,這種迴圈叫巢狀迴圈。
例項1:
[root@node1 ljy]# more ceshi.sh #!/bin/bash for (( a=1;a<5;a++)) do echo "this is $a" for (( b=1;b<4;b++ )) do echo " this is $b" done done [root@node1 ljy]# sh ceshi.sh this is 1 this is 1 this is 2 this is 3 this is 2 this is 1 this is 2 this is 3 this is 3 this is 1 this is 2 this is 3 this is 4 this is 1 this is 2 this is 3
迴圈處理檔案資料
通過IFS環境變數,強制for迴圈將檔案中的每行當成一個條目來處理。
[root@node1 ljy]# more ceshi.sh #!/bin/bash IFS=$'\n' for test in $(cat /etc/passwd) do echo "values is $test----" IFS=: for value in $test do echo "$value" done done [root@node1 ljy]# sh ceshi.sh values is root:x:0:0:root:/root:/bin/bash---- root x 0 0 root /root /bin/bash values is bin:x:1:1:bin:/bin:/sbin/nologin---- bin x 1 1 bin /bin /sbin/nologin
控制迴圈
1、break命令
可以用break命令來跳出任意迴圈,包括while和until迴圈
[root@node1 ljy]# more ceshi.sh #!/bin/bash for (( a=1;a<9;a++ )) do if [ $a -eq 5 ] then break fi echo "number is $a!" done echo "completed!" [root@node1 ljy]# sh ceshi.sh number is 1! number is 2! number is 3! number is 4! completed!
2、continue
continue命令可以提前中止某次迴圈中的命令,但並不會完全中止整個迴圈。
[root@node1 ljy]# more ceshi.sh #!/bin/bash for (( a=1;a<19;a++ )) do if [ $a -gt 5 ]&&[ $a -lt 10 ] then continue fi echo "number is $a!" done echo "completed!" [root@node1 ljy]# sh ceshi.sh number is 1! number is 2! number is 3! number is 4! number is 5! number is 10! number is 11! number is 12! number is 13! number is 14! number is 15! number is 16! number is 17! number is 18! completed!
處理迴圈的輸出
shell可以將for命令的結果重定向到一個新的檔案中,而不是顯示在螢幕上。
[root@node1 ljy]# more ceshi.sh #!/bin/bash for file in /root/* do if [ -d $file ] then echo "$file is a directory!" elif [ -f $file ] then echo "$file is a file!" fi done > out.txt [root@node1 ljy]# sh ceshi.sh [root@node1 ljy]# more out.txt /root/anaconda-ks.cfg is a file! /root/ceshi is a file! /root/one is a file! /root/test.txt is a file!