1

有点跟进这个问题。我使用 bash 进程替换期望看到 5 行输出(对应于 5s 超时)但是我只看到 4 行:

timeout 5s dd if=/dev/random | pv -fr > /dev/null 2> >(tr '\r' '\n' >&2)
[5.42kiB/s]
[1.89kiB/s]
[5.36kiB/s]
[2.41kiB/s]

但是,如果我在中间放置一个命名管道,我会按预期得到 5 行。第一个外壳:

mkfifo testfifo
cat testfifo | tr '\r' '\n' >&2

第二个外壳:

timeout 5s dd if=/dev/random | pv -fr > /dev/null 2> testfifo

第一个 shell 的输出(5 行):

[3.79kiB/s]
[3.12kiB/s]
[ 4.4kiB/s]
[2.05kiB/s]
[5.58kiB/s]

为什么我在使用 bash 进程替换时没有得到 5 行?

奖金

我还尝试将输出转储到文件而不是但这给了我一个空的日志文件:

timeout 5s dd if=/dev/random | pv -fr > /dev/null 2> >(tr '\r' '\n' > rates.log)

我发现在使用时,我的rates.log文件包含最初预期的多行输出:

timeout 5s dd if=/dev/random | pv -fr > /dev/null 2> >(stdbuf -oL tr '\r' '\n' > rates.log)

需要 stdbuf ?(另请注意该文件包含 4 行,因此此调用结构可能会遇到与上述相同的问题)

4

0 回答 0