1 for命令
1 # for:迭代循环;默认空格为分隔符2 3 for var in list4 do5 commands6 done
1.1 读取列表中的值
#!usr/bin/bashfor test in Hello Python Student Schooldo echo The next wprd is $testdoneecho The last state is $test#一直保持最后迭代的值,除非删除(或更改)它
1.2 读取列表中复杂的值
1 # 使用转义字符(反斜线),转义特殊字符2 # 使用双引号定义用到的单引号(或反之)3 # 默认空格为分隔符4 #!usr/bin/bash5 for test in I don\'t know if "this'll" work6 do7 echo "word: $test"8 done
# 双引号创建字符串变量,并作为一个整体出现在列表中$cat fortest01.sh#!usr/bin/bashfor var in "the test bash shell"do echo word: $vardone$sh fortest01.shword: the test bash shell$cat fortest01.sh#!usr/bin/bashfor var in "the test bash shell" "the last test"do echo word: $vardone$sh fortest01.shword: the test bash shellword: the last test
1.3 从变量中读取列表
1 # 注意与直接读取列表的区别: 2 # 列表: 3 * 列表中特殊字符的转义字符 4 * 双引号-特殊字符和字符串变量(整体) 5 # 变量 6 * 定义字符串变量-双引号/单引号 7 * 字符串添加元素 8 * for迭代遍历变量元素 9 $cat varfortest02.sh10 #!usr/bin/bash11 list01='the first test example'12 list02=" the second test example"13 list03=$list02" the thrid test example"14 list04=$list01$list02$list0315 echo $list0416 n=017 for var in $list0418 do19 (( n++ ))20 echo cycle $n: $var21 done22 23 $sh varfortest02.sh24 the first test example the second test example the second test example the thrid test example25 cycle 1: the26 cycle 2: first27 cycle 3: test28 cycle 4: example29 cycle 5: the30 cycle 6: second31 cycle 7: test32 cycle 8: example33 cycle 9: the34 cycle 10: second35 cycle 11: test36 cycle 12: example37 cycle 13: the38 cycle 14: thrid39 cycle 15: test40 cycle 16: example
1.4 从命令读取值
1 # 反引号 2 * 使用文件的绝对路径,除非位于同一个目录 3 * 默认空格为分隔符 4 $cat commandfor.sh 5 #!usr/bin/bash 6 n=0 7 for var in `cat varfortest02.sh` 8 do 9 n=$[ $n+1 ]10 echo line $n: $var11 done12 13 $sh commandfor.sh14 line 1: #!usr/bin/bash15 line 2: list01='the16 line 3: first17 ... ...18 line 24: do19 line 25: ((20 line 26: n++21 line 27: ))22 ... ...23 line 31: $var24 line 32: done
1.5 更改字段分隔符
1 # 环境变量IFS-内部字段分隔符 2 * 默认分隔符:空格;制表符;换行符 3 * 更改:单个:IFS=$'\n'-(换行) 4 多个:IFS=$'\n:;"'-(换行/冒号/分号/双引号) 5 * 保存与恢复: 6 IFS.OLD=$IFS 7 IFS=$'\n' 8 ... 9 IFS=$IFS.OLD 10 11 $cat commandfor.sh12 #!usr/bin/bash13 n=014 IFS.OLD=$IFS15 IFS=$'\n'16 for var in `cat varfortest02.sh`17 do18 n=$[ $n+1 ]19 echo line $n: $var20 done21 IFS=$IFS.OLD22 23 $sh commandfor.sh24 commandfor.sh: line 3: IFS.OLD=: command not found25 line 1: #!usr/bin/bash26 line 2: list01='the first test example'27 line 3: list02=" the second test example"28 line 4: list03=$list02" the thrid test example"29 line 5: list04=$list01$list02$list0330 line 6: echo $list0431 line 7: n=032 line 8: for var in $list0433 line 9: do34 line 10: (( n++ ))35 line 11: echo cycle $n: $var36 line 12: done
1.6 用通配符读取文件/目录
1 # 文件/目录变量尽量用双引号括起来 2 # 文件/目录查找方法和列表方法合并进同一个for语句 * 可以添加任意多个通配符,for语句先匹配文件或目录形成列表,然后遍历列表 3 $cat filefor.sh 4 #!usr/bin/bash 5 for file in ./* ./tsttst 6 do 7 if [ -e "$file" ] 8 then 9 echo The file is $file10 else11 echo The fiel $file do not exist!12 fi13 done14 15 $sh filefor.sh16 The file is ./commandfor.sh17 The file is ./filefor.sh18 The file is ./fortest01.sh19 The file is ./varfortest02.sh20 The fiel ./tsttst do not exist!