Shell 同时读取多个文件

现有两个文件 1.txt  2.txt,内容分别如下:

[root@SHO-XXW-75-136 readmulti]# cat 1.txt 
1
2
3
4
5
6
7
8
9
[root@SHO-XXW-75-136 readmulti]# cat 2.txt 
a
b
c
d
e
f
g
h
i
k
l
m
n

同时读取这两个文件的脚本如下:

1 #!/bin/bash
2 exec 3<"1.txt"
3 exec 4<"2.txt"
4 while read line1<&3 && read line2<&4
5 do
6         echo $line1 $line2
7 done

执行上述脚本,得到如下结果:

1 a
2 b
3 c
4 d
5 e
6 f
7 g
8 h
9 i

可以看出,当同时读取两个行数不同的文件,以上脚本读取的行数以较短的文件为准.

原文地址:https://www.cnblogs.com/tangxin-blog/p/6531812.html