【问题标题】:bash script - How can I redirect the output of time dd command? [duplicate]bash 脚本 - 如何重定向 time dd 命令的输出? [复制]
【发布时间】:2021-12-10 08:35:40
【问题描述】:

我正在编写一个 bash 脚本来通过 time dd 命令对写入/读取速度进行基准测试,时间写入 stderr 同时 dd 写入 stdout,使用 2>&1 我将输出重定向到 stdout 然后我读取在变量 time_writing 中。

dd_output=$(time dd bs="$bs" conv=fsync count=$count if=/dev/zero of="${file_temporaneo:-$block_device}" 2>&1)
write_speed=$(grep --only-matching --extended-regexp --ignore-case '[0-9.]+ ([GMk]?B|bytes)/s(ec)?' <<< "$dd_output")
time_writing=$(sed -n 's/real"\(."\)/\1/p' <<< "$dd_output")

我不知道如何像 dd 命令那样在标准输出中获得输出时间,其中: (time dd if=/dev/urandom of=/dev/null bs=1K count=100000) 2&gt;&amp;1|&amp; sed -n 's/real\t*\(.*\)/\1/p'我只能得到实时,但我也需要写入速度。

这是我从命令中得到的输出:

real    0m1,585s
user    0m0,060s
sys 0m1,352s
       512 -    170 MB/s -           

real    0m0,876s
user    0m0,024s
sys 0m0,719s
      1024 -    307 MB/s -           

real    0m0,584s
user    0m0,020s
sys 0m0,419s
      2048 -    460 MB/s -           

real    0m0,478s
user    0m0,012s
sys 0m0,305s
      4096 -    563 MB/s -           

real    0m0,435s
user    0m0,000s
sys 0m0,281s
      8192 -    618 MB/s -           

real    0m0,379s
user    0m0,000s
sys 0m0,261s
     16384 -    710 MB/s -           

real    0m0,402s
user    0m0,004s
sys 0m0,242s
     32768 -    670 MB/s -           

real    0m0,363s
user    0m0,000s
sys 0m0,232s
     65536 -    742 MB/s -           

real    0m0,387s
user    0m0,000s
sys 0m0,223s
    131072 -    695 MB/s -           

real    0m0,377s
user    0m0,004s
sys 0m0,218s
    262144 -    714 MB/s -           

real    0m0,364s
user    0m0,004s
sys 0m0,217s
    524288 -    741 MB/s -           

real    0m0,443s
user    0m0,000s
sys 0m0,286s
   1048576 -    607 MB/s -           

real    0m0,393s
user    0m0,000s
sys 0m0,260s
   2097152 -    687 MB/s -           

real    0m0,433s
user    0m0,004s
sys 0m0,282s
   4194304 -    623 MB/s -           

real    0m0,496s
user    0m0,000s
sys 0m0,245s
   8388608 -    543 MB/s -           

real    0m0,414s
user    0m0,000s
sys 0m0,246s
  16777216 -    651 MB/s -           

real    0m0,397s
user    0m0,000s
sys 0m0,238s
  33554432 -    681 MB/s -           

real    0m0,439s
user    0m0,000s
sys 0m0,293s
  67108864 -    617 MB/s -

没有时间我得到的是:

   512 -    192 MB/s -           
  1024 -    314 MB/s -           
  2048 -    429 MB/s -           
  4096 -    573 MB/s -           
  8192 -    637 MB/s -           
 16384 -    666 MB/s -           
 32768 -    702 MB/s -           
 65536 -    683 MB/s -           
131072 -    716 MB/s - 

我想得到的是:

     512 -    192 MB/s -     0m0,672s  
      1024 -    314 MB/s -   0m0,572s      
      2048 -    429 MB/s -   0m0,472s        
      4096 -    573 MB/s -   0m0,352s        

等等…… 感谢任何愿意帮助我的人...

【问题讨论】:

标签: bash


【解决方案1】:

修改此命令:

dd_output=$(time dd bs="$bs" conv=fsync count=$count if=/dev/zero of="${file_temporaneo:-$block_device}" 2>&1)

像这样包含time产生的输出:

dd_output=$( { time dd bs="$bs" conv=fsync count=$count if=/dev/zero of="${file_temporaneo:-$block_device}"; } 2>&1 )

【讨论】:

  • How to Answer 中,请注意回答正确提出的问题 部分,以及其中有关“之前已被多次询问和回答”的问题的要点。跨度>
  • 非常感谢@Maxxim,我一直在查看所有堆栈溢出的答案,但不知道该怎么做......
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多