Bash process substitution behaves differently than named pipe?

房东的猫 提交于 2020-06-16 16:58:47

问题


Kind of a follow up to this question. I use bash process substitution expecting to see 5 lines of output (corresponding to 5s timeout) however I only see 4 lines:

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]

However if I place a named pipe in the middle I do get the 5 lines as expected. First shell:

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

Second shell:

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

Output from first shell (5 lines):

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

Why do I not get 5 lines when using bash process substitution?

BONUS

I also attempted to dump the output to a file instead of stderr however this gave me an empty log file:

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

I discovered that when using stdbuf my rates.log file contain multiple lines of output as originally expected:

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

Why was the stdbuf required for my tr? (also notice the file contains 4 lines so this call structure likely suffers the same problem as above)

来源:https://stackoverflow.com/questions/61901856/bash-process-substitution-behaves-differently-than-named-pipe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!