能够同时读取两个文件

时间:2014-04-13 06:31:20

标签: bash file unix io-redirection

我想从fileA和fileB输出以下内容:

fileA: a b c d e
fileB: 1\t2\t3\ta b c d e
fileA: f g h i j
fileB: 4\t5\t6\tf g h i j
fileA: k l m n o
fileB: 7\t8\t9\tk l m n o

但是,我的脚本输出以下内容(我无法弄清楚原因):

fileA: a b c d e
fileB: 1\t2\t3\ta b c d e\n1 2 3 a b c d e
fileA: f g h i j
fileB: 4\t5\t6\tf g h i j\n4 5 6 f g h i j
fileA: k l m n o
fileB: 7\t8\t9\tk l m n o\n7 8 9 k l m n o

的fileA:

a b c d e
f g h i j
k l m n o

FILEB:

1<tab>2<tab>3<tab>a b c d e
4<tab>5<tab>6<tab>f g h i j
7<tab>8<tab>9<tab>k l m n o

script.sh:

#!/bin/bash

while :
do
    read A <&3
    read B <&4
    [  -z "${A}" -a -z "${B}" ] && break
    echo "fileA: ${A}"
    echo "fileB: ${B}"
done 3<fileA 4<fileB

请注意,<tab>是指文件中的实际标签。

2 个答案:

答案 0 :(得分:0)

尝试这样读:

read -u 3 A
read -u 4 B

答案 1 :(得分:0)

原来我是个白痴。在while循环中有一个额外的echo语句(我忘了在代码中注释掉)。