Search code examples
bashshellnetstat

Shell Script - Alert if netstat output does not exal 0 for all lines


How can I look at the output of the third column in netstat -an results and if any line does not equal 0 then alert.

For Example

netstat -an|grep 100|awk '{print $3}'
0
0
0
2322
0
0
4344
0

Should result in "Alert, netstat is showing 2 numbers greater than zero"

I'm just trying to alert if any row is something other than 0.

Thanks!


Solution

  • Combined with AlexP's answer, you'll get:

    if [ $(netstat -an|grep 100|awk '{print $3}' | grep -vc '^0$') -ge 2 ]; then
        echo "Alert, netstat is showing 2 numbers greater than zero"
    fi