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

可以看出,當同時讀取兩個行數不同的文件,以上腳本讀取的行數以較短的文件為准.


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM