You don't need the sed calls to remove empty lines since the grep will do that (since it only matches lines with at least one digit). And you don't need the files, either. Also, the variables THREE and NLINES are both unnecessary. If the idea with THREE was to name a "magic number" then it really doesn't accomplish much! Maybe LIMIT, but I think a plain 3 is best.
Code:
#!/bin/bash
LINES=$(
netstat -ant |
awk '{print $5}' |
awk -F":" '{print $2}' |
grep -E "[0-9]" |
awk '!x[$0]++' |
awk 'END {print NR}' )
if [ $LINES -lt 3 ]
then
echo "You're safe"
else
echo "[danger]"
/path/to/ui_executable
fi
It's still pretty inefficient, 6 program invocations (4 awks!).
A possibility that doesn't use awk is this (but I don't know what the cut columns should be since I'm not running Linux at the moment; so you'll have to adjust the 50-70 to cut the correct columns).
Code:
LINES=$(
netstat -ant |
cut -c 50-70 |
grep -Eo ":[0-9]+" |
sort -u |
wc -l )