awk exit code if regular expression did not match
You can set a variable to hold the return code, then negate the variable before quitting:
netstat -nap |
grep "LISTEN\b" |
awk '$4 ~ /:80$/ {rc = 1; print $NF}; END { exit !rc }'
If you don't need \b
, then you can remove grep
part:
netstat -nap | awk '/LISTEN/ && $4 ~ /:80$/ {rc = 1; print $NF}; END { exit !rc }'
for me, this is awk's unforgiveable sin; if in need of an error status, I pipe to grep dot, as opposed to the awk gymnastics above
$ date | awk '/Fri/'
Fri May 29 21:26:18 EDT 2020
$ date | awk '/Fri/' | grep .
Fri May 29 21:27:11 EDT 2020
$ echo $?
0
$ date | awk '/Mon/' | grep .
$ echo $?
1