for命令
bash shell提供了for命令,容許你建立一個遍歷一系列的循環。node
for var in list do commands done
一、讀取列表中的值
for命令最基本的用法就是遍歷for命令自身所定義的一系列值。shell
[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
二、讀取列表中的複雜值
若是列表中數值比較麻煩,for命令可能會識別異常。bash
[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循環假定每一個值都是空格分割,若是要包含空格的數值,能夠用""雙引號。this
三、從變量讀取列表
能夠將一些列的值儲存在變量中,而後遍歷變量中的整個列表。spa
[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
四、從命令讀取值
[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
五、更改字段分隔符
IFS(內部字段分隔符)環境變量定義了bash shell用作字段分隔符的一些列字符,默認狀況下,bash shell將如下字符當作字段分隔符:code
一、空格blog
二、製表符for循環
三、換行符class
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=:
六、通配符讀取目錄
能夠用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
控制循環
一、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!
二、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!