shell下同时读取多个文件的方法(2)
多文件读取示例代码如下: readfiles ( ) { local FD1 = 7 local FD2 = 8 local file1 = $1 local file2 = $2 local count1 = 0 local count2 = 0 local eof1 = 0 local eof2 = 0 local data1 local dat
多文件读取示例代码如下:
readfiles() { local FD1=7 local FD2=8 local file1=$1 local file2=$2 local count1=0 local count2=0 local eof1=0 local eof2=0 local data1 local data2 # Open files. exec 7<$file1 exec 8<$file2 while [[ $eof1 -eq 0 || $eof2 -eq 0 ]] do if read data1<&$FD1; then let count1++ printf "%s, line %d: %s\n" $file1 $count1 "$data1" else eof1=1 fi if read data2 <&$FD2; then let count2++ printf "%s, line %d: %s\n" $file2 $count2 "$data2" else eof2=1 fi done } #read file1 and file2 readfiles file1 file2
精彩图集
精彩文章