Bash makes it easy to enumerate all the drives:
$ echo /dev/sd{a..h}
/dev/sda /dev/sdb /dev/sdc /dev/sdd /dev/sde /dev/sdf /dev/sdg /dev/sdh
Then you said you wanted to average the timing output, so let's define a function to do that:
perform_timing() {
for i in {1..3}; do hdparm -t "$1"; done |
awk '/seconds/ { total += $11; count++ } END { print (total / count) }'
}
Then you can run it on all the drives:
for drive in /dev/sd{a..h}; do
printf '%s: %s\n' "$drive" "$(perform_timing "$drive")"
done
Beaking it Down
The perform_timing
function does two things: 1) runs hdparm
three times, then 2) averages the output. You can see how the first part works by running it manually:
# for i in {1..3}; do hdparm -t "/dev/sdc"; done
/dev/sdc:
Timing buffered disk reads: 1536 MB in 3.00 seconds = 511.55 MB/sec
/dev/sdc:
Timing buffered disk reads: 1536 MB in 3.00 seconds = 511.97 MB/sec
/dev/sdc:
Timing buffered disk reads: 1538 MB in 3.00 seconds = 512.24 MB/sec
The second part combines your awk
code with logic to average all the lines, instead of printing them individually. You can see how the averaging works with a simple awk
example:
$ printf '1\n4\n5\n'
1
4
5
$ printf '1\n4\n5\n' | awk '{ total += $1; count++ } END { print (total / count) }'
3.33333
We wrap all that logic in a function called perform_timing
as a good programming practice. That lets us call it as if it were any other command:
# perform_timing /dev/sdc
512.303
Finally, instead of writing:
perform_timing /dev/sda
perform_timing /dev/sdb
...
We wrap it all in a loop, which this simplified loop should help explain:
# for drive in /dev/sd{a..c}; do printf '%s\n' "$drive"; done
/dev/sda
/dev/sdb
/dev/sdc