1. 程式人生 > >【Shell】Shell程式設計之for迴圈命令

【Shell】Shell程式設計之for迴圈命令

bash shell提供了for命令,用於建立通過一系列值重複的迴圈,for命令的格式為:
for var in list
do
   commands
done
在引數list中提供了一系列用於迭代的 值,變數$var包含當前迭代的列表項值。 1、讀取列表中的值 for命令的最基本用法是通過在for命令中定義一列值來迭代:
[[email protected] bash_stu]# cat test1.sh 
#!/bin/bash
#basic for command

for var in China Japan USA UK Canada 
do
   echo The next contry is $var
done

[
[email protected]
bash_stu]# . test1.sh The next contry is China The next contry is Japan The next contry is USA The next contry is UK The next contry is Canada [[email protected] bash_stu]#
最後一次迭代後,變數$var在shell指令碼的其他地方仍然有效,除非改變它的值。
[[email protected] bash_stu]# cat test1.sh 
#!/bin/bash

#basic for command

for var in China Japan USA UK Canada 
do
   echo The next contry is $var
done

echo The last value for contry is $var

var=Alen

echo The last value is $var
[
[email protected]
bash_stu]# . test1.sh The next contry is China The next contry is Japan The next contry is USA The next contry is UK The next contry is Canada The last value for contry is Canada The last value is Alen [[email protected] bash_stu]#
2、讀取列表中的複雜值 for迴圈每個值用空格分割,當列表中有單引號或空格時,遍歷時會出現錯誤的結果:
[[email protected] bash_stu]# cat test2.sh 
#!/bin/bash
#basic for command

for var in Welcome to New York It's my baby's dog  -- New York為一個整體
do
   echo The word is $var
done
[[email protected] bash_stu]# . test2.sh 
The word is Welcome
The word is to
The word is New
The word is York
The word is Its my babys
The word is dog
[[email protected] bash_stu]# 
顯示的結果不是我們想要的,有兩種方法可以解決這個問題:
  • 使用轉義字元(\);
  • 使用雙引號定義使用單引號的值。
[[email protected] bash_stu]# cat test2.sh 
#!/bin/bash

#basic for command

for var in Welcome to "New York" It\'s my "baby's" dog  --分別使用雙引號和轉義解決
do
   echo The word is $var
done
[[email protected] bash_stu]# . test2.sh 
The word is Welcome
The word is to
The word is New York
The word is It's
The word is my
The word is baby's
The word is dog
[[email protected] bash_stu]# 
3、從變數讀取列表
[[email protected] bash_stu]# cat test2.sh 
#!/bin/bash

#basic for command

list="Welcome to learn shell"
list=$list" Alen"  --將一個變數增加到一個已存在的變數中

for var in $list  
do
   echo The word is $var
done
[[email protected] bash_stu]# . test2.sh 
The word is Welcome
The word is to
The word is learn
The word is shell
The word is Alen
[[email protected] bash_stu]# 
4、讀取命令中的值 使用反引號可以讀取命令的結果,例如:
[[email protected] bash_stu]# cat test2.sh 
#!/bin/bash
#basic for command

f="country.txt"

for var in `cat $f`  
do
   echo The word is $var
done
[[email protected] bash_stu]# cat country.txt 
China
Japan
USA
UK
[[email protected] bash_stu]# . test2.sh 
The word is China
The word is Japan
The word is USA
The word is UK
[[email protected] bash_stu]# 
5、改變欄位分隔符 環境變數IFS(內部欄位分隔符)定義了shell用作欄位分隔符的字元列表,預設情況下,shell將下面的字元看做欄位分隔符:空格、製表符和換行符。
[[email protected] bash_stu]# cat test2.sh 
#!/bin/bash
#basic for command

f="country.txt"

IFS=$'\n'  --重新定義欄位分隔符,忽略空格和製表符
for var in `cat $f`  
do
   echo The word is $var
done
[[email protected] bash_stu]# . test2.sh 
The word is China Beijing
The word is Japan 
The word is USA New York
The word is UK
[[email protected] bash_stu]# 
為了能使IFS值可以返回預設值,可以改為如下方式: IFS.OLD=$IFS IFS=$'\n' <使用新的IFS處理語句> IFS=$IFS.OLD 6、使用萬用字元讀取目錄
[[email protected] bash_stu]# cat test2.sh 
#!/bin/bash
#basic for command

for var in /root/bash_stu/* 
do
   if [ -d $var ]
   then
      echo "$var is a directory"
   elif [ -f $var ]
   then
      echo "$var is a file"
   fi
done
[[email protected] bash_stu]# . test2.sh 
/root/bash_stu/country.txt is a file
/root/bash_stu/test1.sh is a file
/root/bash_stu/test2.sh is a file
/root/bash_stu/tt is a directory
[[email protected] bash_stu]# 
7、C 語言中的for命令
[[email protected] bash_stu]# cat test2.sh 
#!/bin/bash

v=1;
for((i=1;i<5;i++))
do
  echo $v + $i = $(($v+$i))  
done
[[email protected] bash_stu]# . test2.sh 
1 + 1 = 2
1 + 2 = 3
1 + 3 = 4
1 + 4 = 5
[[email protected] bash_stu]# 
8、for迴圈使用多個變數
[[email protected] bash_stu]# cat test2.sh 
#!/bin/bash

for((i=1,j=5;i<5;i++,j--))
do
  echo $i + $j = $(($i+$j))  
done
[[email protected] bash_stu]# . test2.sh 
1 + 5 = 6
2 + 4 = 6
3 + 3 = 6
4 + 2 = 6
[[email protected] bash_stu]# 
9、練習,輸出100之內能被3整除的數
[[email protected] bash_stu]# cat test2.sh 
#!/bin/bash

for ((i=1;i<=100;i++))
do
  if [ $(($i%3)) -eq 0 ]
  then
    echo "$i"
  fi
done
[[email protected] bash_stu]# . test2.sh 
3
6
9
12
15
18
21
24
27
--------------------省略---------------------
10、練習巢狀迴圈,列印9*9以內乘法表
[[email protected] bash_stu]# cat test2.sh 
#!/bin/bash

for((i=1;i<=9;i++))
do
  for((j=1;j<=i;j++))
  do
     echo -n "$i*$j=$(($i*$j))   "
  done
  echo ""
done
[[email protected] bash_stu]# . test2.sh 
1*1=1   
2*1=2   2*2=4   
3*1=3   3*2=6   3*3=9   
4*1=4   4*2=8   4*3=12   4*4=16   
5*1=5   5*2=10   5*3=15   5*4=20   5*5=25   
6*1=6   6*2=12   6*3=18   6*4=24   6*5=30   6*6=36   
7*1=7   7*2=14   7*3=21   7*4=28   7*5=35   7*6=42   7*7=49   
8*1=8   8*2=16   8*3=24   8*4=32   8*5=40   8*6=48   8*7=56   8*8=64   
9*1=9   9*2=18   9*3=27   9*4=36   9*5=45   9*6=54   9*7=63   9*8=72   9*9=81   
[[email protected] bash_stu]# 
11、處理迴圈的輸出 可以使用管道或者重定向迴圈輸出結果,也可以在done後新增處理命令做到。
[[email protected] bash_stu]# cat test2.sh 
#!/bin/bash

for((i=1;i<=9;i++))
do
  for((j=1;j<=i;j++))
  do
     echo -n "$i*$j=$(($i*$j))   "
  done
  echo ""
done > mu.txt
[[email protected] bash_stu]# . test2.sh 
[[email protected] bash_stu]# cat mu.txt 
1*1=1   
2*1=2   2*2=4   
3*1=3   3*2=6   3*3=9   
4*1=4   4*2=8   4*3=12   4*4=16   
5*1=5   5*2=10   5*3=15   5*4=20   5*5=25   
6*1=6   6*2=12   6*3=18   6*4=24   6*5=30   6*6=36   
7*1=7   7*2=14   7*3=21   7*4=28   7*5=35   7*6=42   7*7=49   
8*1=8   8*2=16   8*3=24   8*4=32   8*5=40   8*6=48   8*7=56   8*8=64   
9*1=9   9*2=18   9*3=27   9*4=36   9*5=45   9*6=54   9*7=63   9*8=72   9*9=81   
[[email protected] bash_stu]#