I have the following line in a Nagios bash script. It is being used to get the up and down error rates for the specified network cards:
if=`awk -v interface="$INTERFACE" '$1 ~ "^" interface ":" { split($0, a, /: */); $0 = a[2]; print $3 " " $11 }' /proc/net/dev`
I've never worked with awk before today, so I'm finding my way a bit.
As I see it, we pass the value $INTERFACE into the awk script as interface, and then fil开发者_开发问答ter for lines beginning interface:
(eg eth0:). Then, we split the line using colon-space as a separator. Then, for some reason we assign the third entry in the array to $0 before actually extracting the values we want.
It seems to me that the statements split($0, a, /: */)
and $0 = a[2]
are unecessary but I may be wrong! Does the assigning of a[2] to $0 change anything when we then refer to $3 and $11? I've tried the script without the first two statements and the output is the same, but perhaps there's a corner case I've missed...
Thanks in advance
Rich
The split() is unnecessary. This is the same as your awk statement
awk -v interface="eth0" '$1~interface{print $3,$11 }' /proc/net/dev
alternatively, you can use the shell(bash/ksh)
shopt -s extglob
var=$(< /proc/net/dev)
var="${var##*$interface:+( )}" # remove everything until and including the interface
var="${var%%$'\n'*}" #remove from first newline onwards
set -- $var
echo "$3 ${11}"
精彩评论