2

How can I print the output of to a file? For example:

timeout 5s dd if=/dev/random | pv -r > /dev/null
[ 505kiB/s]

The rate line output is "updated" over the course of my five second timeout. I tried this but it does not work (log is empty):

timeout 5s dd if=/dev/random | pv -r > /dev/null 2> rates.log

I believe it has something to do with carriage returns in the output, but after an hour I am stuck. Ideally my log file would have multiple lines each time prints a new value:

[ 505kiB/s]
[ 498kiB/s]
[ 542kiB/s]
[ 513kiB/s]
[ 509kiB/s]

UPDATE:

To get the content into a file as I described above I had to use though I'm not sure why it is required ( alone didn't work, the file will be empty without ):

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

1 Answers1

3

From man pv:

-f, --force
Force output. Normally, pv will not output any visual display if standard error is not a terminal. This option forces it to do so.

Since rates.log isn't a terminal, you need to do pv -fr instead of pv -r.

  • I posted a [follow up question](https://stackoverflow.com/questions/61901856/bash-process-substitution-behaves-differently-than-named-pipe) if you are interested. Thanks! – dtmland May 20 '20 at 15:02