Linux – bash script how to parse outputs

bashddlinuxnfs

I am writing a script to test read/write performance on the network storage, but i need a bit of help to finish the script.

The script is simple:

  1. reloads the nfs mount to clear the cache
  2. write a test file to nfs
  3. record time
  4. read a test file from nfs
  5. record time

I have one remaining issues to solve: parse the output of the time command and store it in a text file.

Time command outputs three values:
real 0m0.000s
user 0m0.000s
sys 0m0.000s

I just want the real time. and one file for read and one file for write.

This is what I have so far:

#!/bin/bash
for i in [`seq 1 20`]; 
echo "remounting autofs"
/etc/init.d/autofs reload;
wait 5;
echo "write test"
#for write perf
do time dd if=/dev/zero of=/home/nfs_perf_testing/samplefile$i bs=1M count=1024 oflag=direct;

echo "write test done";

wait 5;
echo "read test";
#for read perf;
do time dd if=/home/nfs_perf_testing/samplefile of=/dev/null bs=1M count=1024 iflag=direct;

echo "read test done";
done;

Thank you all

Best Answer

time(1) prints to stderr so you need to redirect it's output to stdout 2>&1, then pipe that to grep to find the line you want grep real. Then lastly use awk to print the column you want awk '{ print $2 }'.

This should put together look like:

(time command_to_time) 2>&1 | grep real | awk '{ print $2 }'
Related Question