DD Command Output – How to Send to awk/sed/grep

bashddio-redirectionpythonshell

I'm trying to approximate the computer's write speed using dd:

dd if=/dev/urandom of=/dev/null bs=1K count=10000

which gives the following output

10000+0 records in
10000+0 records out
10240000 bytes (10 MB) copied, 0.746313 s, 13.7 MB/s

How can I get the '13.7 MB/s' into a bash variable? I've tried piping the output from dd to progs like awk, sed and grep to no avail.

Ultimately, I'm calling this via os.system(...) from within a python script. If anyone knows of a more direct way to get a similar result inside python I'd be interested in that also. I'm trying to predict how long a file copy will take, based on the file size.

Any help would be greatly appreciated.

Best Answer

The problem is your designated output from dd goes to STDERR and not STDOUT so you have to redirect STDERR as well and not only STDOUT.

For bash and zsh you can use |& instead of | which will also redirect STDERR to STDIN of the second command, e.g:

dd if=/dev/urandom of=/dev/null bs=1K count=10000 |& awk '/copied/ {print $8 " "  $9}'

The more general approach is to redirect STDERR explicitly with 2>&1, e.g:

dd if=/dev/urandom of=/dev/null bs=1K count=10000 2>&1 | awk '/copied/ {print $8 " "  $9}'

For the python part have a look at at the subprocess module and Popen in particular.

Related Question