Posted onInprogramSymbols count in article: 2.8kReading time ≈3 mins.
今天研究apache ab这个测试工具,在网上看到压力测试shell脚本一文介绍了一个封装的bash脚本,用于多次测试返回requests per second的平均值,对脚本进行了简单的改写,将所有的测试输出进行记录。改写脚本在文章的最后。
改写过程中发现这样一个问题,比如写下面的脚本:
1 2 3 4 5 6 7 8 9 10 11 12
#!/bin/bash
result="" cat abtest_temp.log | whileread LINE do result=`echo$LINE | grep 'Requests per second:'` if [ "$result" != "" ] then break fi done echo"result is "${result}
result="" cat abtest_temp.log | whileread LINE do result=`echo$LINE | grep 'Requests per second:'` if [ "$result" != "" ] then echo$result > .result_temp break fi done echo"result is "`cat .result_temp`
使用命名管道
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#!/bin/bash
result="" mkfifo pipetem (cat abtest_temp.log | whileread LINE do result=`echo$LINE | grep 'Requests per second:'` if [ "$result" != "" ] then echo$result > pipetem & break fi done) read result < pipetem rm pipetem echo"result is "${result}