Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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.

share|improve this question
    
Have a look at File descriptors & shell scripting –  Ulrich Dangel Jul 24 '12 at 22:12

2 Answers 2

up vote 6 down vote accepted

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.

share|improve this answer
    
Thanks very much, it worked a treat! –  James R Jul 24 '12 at 22:16
var=$(dd if=/dev/urandom of=/dev/null bs=1K count=10000 2>&1)
var=$(echo ${var##*,})
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.