问题
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