【问题标题】:able to read two files simultaneously能够同时读取两个文件
【发布时间】:2014-05-27 05:20:41
【问题描述】:

我想从 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

文件A:

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

文件B:

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

请注意,&lt;tab&gt; 指的是文件中的实际选项卡。

【问题讨论】:

  • 它也适用于我。您的某个文件是否有有趣的行尾?
  • 如果任一文件已通过基于 Windows 的系统,最好使用 dos2unix file [file2 ....] 将行尾转换为 Unix 格式。祝你好运。
  • 这很奇怪。实际上,我在这里写了确切的 fileA 和 fileB,以及这里的确切脚本(全部在 Unix 中)。我在emacs中输入了它们。 @merlin2011 - 我应该期待什么样的有趣的行尾?
  • @shelter - 我尝试了“dos2unix fileA”和“dos2unix fileB”,但仍然得到相同的行为。

标签: bash file unix io-redirection


【解决方案1】:

尝试这样阅读:

read -u 3 A
read -u 4 B

【讨论】:

    【解决方案2】:

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

    【讨论】:

      最近更新 更多