2

我一直在尝试在我的服务器上使用以下命令 dd if=/dev/zero bs=1M count=1024 | md5sum

输出:

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

如何让它367 MB/s仅将速度()显示为输出?状态打印到stderr

我目前正在使用awk,但它显示了 md5 哈希。

帮助表示赞赏:)

4

1 回答 1

4

首先,一个模拟你的命令的函数

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  -

然后,解决方案:将 stderr 重定向到将所需输出显示回 stderr 的进程替换,在变量中捕获 stdout。

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

$ echo $md5sum
cd573cfaace07e7949bc0c46028904ff -
于 2014-02-22T04:00:53.953 回答