在shell脚本

时间:2016-02-17 10:24:24

标签: shell unix

我编写了一个脚本,它在for循环中读取2个文件并调用实用程序脚本。最后,脚本将调用合并报表函数以表格格式显示输出。

我的问题是无法弄清楚如何一个接一个地显示结果和用户详细信息。非常感谢任何帮助。

  function runClients {

if [ ! -f $RFILE ]; then
    echo -e "rfile doesn't exits.. pls check!! exiting"
    exit 1
fi
for t in ` cat $TFILE ` ; do
  for r in ` cat $RFILE ` ; do
  echo "Running client for token: ${t} on item: {$r}" | tee -a $RESULT_FILE
  ${TESTCLIENT} -f $r -u $t ${TEST_LOG_DIR}/${r}_`date '+%Y%m%d_%H%M%S'`.xml >> $RESULT_FILE
   if [ -s $RESULT_FILE ]; then
      STATUS=${STATUS_OK}
     else
      STATUS=${STATUS_NOK}
   fi
  done
 done
 consolidated_report
 sleep $SLEEPTIME # Assuming it needs at least 30 seconds for test client to complete

}

consolidated_report函数:

    function consolidated_report () {
  printf "|===========================================================================|\n";
  printf "Displaying SummaryReport\n"
  echo "Executed on Date : " `date  "+%m/%d/%y %H:%M:%S"`
if [[ -r $RESULT_FILE && -s $RESULT_FILE  ]]; then
  printf "|==========================================================================|\n"; 
  printf "|              Round Trip Summary                                          |\n"
  printf "|==========================================================================|\n";
  cat $RESULT_FILE
  echo -e "Testcase status :\e[1;32m ${STATUS_OK}\e[0m                                                    "
  printf "|===========================================================================|\n";

fi
}

目前正在获得如下输出:

    Running testclient for user: AQIC5wM2LY4SfcxZae% on item: {ffed}
Running testclient for user: Q3MTkxMDkwOQACUzEAAjA2%23 on item: {rrcw}
|===========================================================================|
Displaying SummaryReport
Executed on Date :  02/17/16 09:39:59
|==========================================================================|
|              Round Trip Summary                                          |
|==========================================================================|
Number of Connection Count = 1
Items Requested: 2 Item Retrieval Time: 86 milliseconds.
Average Round Trip : 65 milliseconds
Minimum Round Trip : 44 milliseconds for item: ffed
Maximum Round Trip : 86 milliseconds for item: rrcw
Item retrieval complete. Exiting.
Channel: 1 Disconnected application from server.
Peak Rate: 0.00 Peak Average Update Size: 0
Number of Connection Count = 1
Items Requested: 2 Item Retrieval Time: 83 milliseconds.
Average Round Trip : 62 milliseconds
Minimum Round Trip : 42 milliseconds for item: ffed
Maximum Round Trip : 83 milliseconds for item: rrcw
Item retrieval complete. Exiting.
Channel: 1 Disconnected application from server.
Peak Rate: 0.00 Peak Average Update Size: 0
Testcase status : Passed
|===========================================================================|

我希望得到摘要报告:

    |===========================================================================|
Displaying SummaryReport
Executed on Date :  02/17/16 09:39:59
|==========================================================================|
|              Round Trip Summary                                          |
|==========================================================================|
Running testclient for user: AQIC5wM2LY4SfcxZae% on item: {ffed}
Number of Connection Count = 1
Items Requested: 2 Item Retrieval Time: 86 milliseconds.
Average Round Trip : 65 milliseconds
Minimum Round Trip : 44 milliseconds for item: ffed
Maximum Round Trip : 86 milliseconds for item: rrcw
Item retrieval complete. Exiting.
Channel: 1 Disconnected application from server.
Peak Rate: 0.00 Peak Average Update Size: 0
Running testclient for user: Q3MTkxMDkwOQACUzEAAjA2%23 on item: {rrcw}
Number of Connection Count = 1
Items Requested: 2 Item Retrieval Time: 83 milliseconds.
Average Round Trip : 62 milliseconds
Minimum Round Trip : 42 milliseconds for item: ffed
Maximum Round Trip : 83 milliseconds for item: rrcw
Item retrieval complete. Exiting.
Channel: 1 Disconnected application from server.
Peak Rate: 0.00 Peak Average Update Size: 0
Testcase status : Passed
|===========================================================================|

1 个答案:

答案 0 :(得分:1)

由于每次调用后总会有一些延迟,您可以将单个调用的输出分配到单独的输出文件中,之后您可以将它们与另一个小代码联合起来;

for t in ` cat $TFILE ` ; do
  for r in ` cat $RFILE ` ; do
    touch $RESULT_FILE.$t.$r
    echo "Running client for token: ${t} on item: {$r}" | tee -a $RESULT_FILE.$t.$r
    ${TESTCLIENT} -f $r -u $t ${TEST_LOG_DIR}/${r}_`date '+%Y%m%d_%H%M%S'`.xml >> $RESULT_FILE.$t.$r
    if [ -s $RESULT_FILE ]; then
      STATUS=${STATUS_OK}
    else
      STATUS=${STATUS_NOK}
    fi
  done
done

因此,对于您示例中的第一个调用,您将获得一个带有名称的输出文件; $RESULT_FILE.AQIC5wM2LY4SfcxZae%.{ffed},它首先包含小回声,然后是客户端调用的输出。之后,您可以将这些文件连接到$RESULT_FILE,然后调用consolidated_report

或者你可以为每个调用(一个)更少杂乱的文件名提供索引和增量; $RESULT_FILE$i

虽然凌乱的文件名无关紧要,因为那些只是临时文件。