[linux] 结构化命令-for

1 for命令

1 # for:迭代循环;默认空格为分隔符
2 
3 for var in list
4 do
5     commands
6 done

  1.1 读取列表中的值

#!usr/bin/bash
for test in Hello Python Student School
do
    echo The next wprd is $test
done
echo The last state is $test
#一直保持最后迭代的值,除非删除(或更改)它

  1.2 读取列表中复杂的值

1 # 使用转义字符(反斜线),转义特殊字符
2 # 使用双引号定义用到的单引号(或反之)
3 # 默认空格为分隔符
4 #!usr/bin/bash
5 for test in I don't know if "this'll" work
6 do
7     echo "word: $test"
8 done
# 双引号创建字符串变量,并作为一个整体出现在列表中

$cat fortest01.sh
#!usr/bin/bash
for var in "the test bash shell"
do
        echo word: $var
done

$sh fortest01.sh
word: the test bash shell

$cat fortest01.sh
#!usr/bin/bash
for var in "the test bash shell" "the last test"
do
        echo word: $var
done

$sh fortest01.sh
word: the test bash shell
word: the last test

1.3 从变量中读取列表

 1 # 注意与直接读取列表的区别:
 2     # 列表:
 3         * 列表中特殊字符的转义字符
 4         * 双引号-特殊字符和字符串变量(整体)
 5     # 变量
 6         * 定义字符串变量-双引号/单引号
 7         * 字符串添加元素
 8         * for迭代遍历变量元素
 9 $cat varfortest02.sh
10 #!usr/bin/bash
11 list01='the first test example'
12 list02=" the second test example"
13 list03=$list02" the thrid test example"
14 list04=$list01$list02$list03
15 echo $list04
16 n=0
17 for var in $list04
18 do
19         (( n++ ))
20         echo cycle $n: $var
21 done
22 
23 $sh varfortest02.sh
24 the first test example the second test example the second test example the thrid test example
25 cycle 1: the
26 cycle 2: first
27 cycle 3: test
28 cycle 4: example
29 cycle 5: the
30 cycle 6: second
31 cycle 7: test
32 cycle 8: example
33 cycle 9: the
34 cycle 10: second
35 cycle 11: test
36 cycle 12: example
37 cycle 13: the
38 cycle 14: thrid
39 cycle 15: test
40 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: $var
11 done
12 
13 $sh commandfor.sh
14 line 1: #!usr/bin/bash
15 line 2: list01='the
16 line 3: first
17 ... ...
18 line 24: do
19 line 25: ((
20 line 26: n++
21 line 27: ))
22 ... ...
23 line 31: $var
24 line 32: done

1.5 更改字段分隔符

 1 # 环境变量IFS-内部字段分隔符
 2         * 默认分隔符:空格;制表符;换行符
 3         * 更改:单个:IFS=$'
'-(换行)
 4                多个:IFS=$'
:;"'-(换行/冒号/分号/双引号)
 5         * 保存与恢复:
 6                 IFS.OLD=$IFS
 7                 IFS=$'
'
 8                 ...
 9                 IFS=$IFS.OLD    
10 
11 $cat commandfor.sh
12 #!usr/bin/bash
13 n=0
14 IFS.OLD=$IFS
15 IFS=$'
'
16 for var in `cat varfortest02.sh`
17 do
18         n=$[ $n+1 ]
19         echo line $n: $var
20 done
21 IFS=$IFS.OLD
22 
23 $sh commandfor.sh
24 commandfor.sh: line 3: IFS.OLD=: command not found
25 line 1: #!usr/bin/bash
26 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$list03
30 line 6: echo $list04
31 line 7: n=0
32 line 8: for var in $list04
33 line 9: do
34 line 10:        (( n++ ))
35 line 11:        echo cycle $n: $var
36 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 $file 10 else 11 echo The fiel $file do not exist! 12 fi 13 done 14 15 $sh filefor.sh 16 The file is ./commandfor.sh 17 The file is ./filefor.sh 18 The file is ./fortest01.sh 19 The file is ./varfortest02.sh 20 The fiel ./tsttst do not exist!
原文地址:https://www.cnblogs.com/xiaofeiIDO/p/6214612.html