現有兩個文件 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
可以看出,當同時讀取兩個行數不同的文件,以上腳本讀取的行數以較短的文件為准.