2

I've been trying to use the following command on my server dd if=/dev/zero bs=1M count=1024 | md5sum

The output:

1024+0 records in
1024+0 records out
1073741824 bytes (1.1 GB) copied, 2.92245 s, 367 MB/s
cd573cfaace07e7949bc0c46028904ff  -

How do I let it show the speed (367 MB/s) as output only? The status is printed to stderr.

I'm currently using awk but it showed the md5 hash.

Helps are appreciated :)

Zombo
  • 1
  • 62
  • 391
  • 407
user2248259
  • 316
  • 1
  • 3
  • 13

1 Answers1

4

First, a function to simulate your command

simulation() { 
  echo "1024+0 records in
1024+0 records out
1073741824 bytes (1.1 GB) copied, 2.92245 s, 367 MB/s" >&2
  echo "cd573cfaace07e7949bc0c46028904ff  -"
}

$ simulation >/dev/null
1024+0 records in
1024+0 records out
1073741824 bytes (1.1 GB) copied, 2.92245 s, 367 MB/s

$ simulation 2>/dev/null
cd573cfaace07e7949bc0c46028904ff  -

Then, the solution: redirecting stderr to a process substitution that displays the desired output back to stderr, capturing stdout in a variable.

$ md5sum=$( simulation 2> >(sed -n '/MB\/s/ {s/.*, //p; q}' >&2) )
367 MB/s

$ echo $md5sum
cd573cfaace07e7949bc0c46028904ff -
glenn jackman
  • 238,783
  • 38
  • 220
  • 352
  • 1
    To clarify, you need to replace `simulation` with just the `dd` command, and put the `| md5sum` right *before* the last closing parenthesis, ie: `>&2) | md5sum )` – Martin Tournoij Feb 22 '14 at 04:06