2

I am trying to count the amount of connections while groupping them by their 'state'.

This command achieve that goal :

netstat -ant | awk '{ print $6}' | sort | uniq -c

which provide an output that looks like that :

  4 CLOSE_WAIT
  1 established)
127 ESTABLISHED
  1 Foreign
  2 LAST_ACK
 39 LISTEN
  9 TIME_WAIT

I am trying to combine my command with the watch command like that :

watch -n 1 "netstat -ant | awk '{ print $6}' | sort | uniq -c"

But, the output is just of the netstat -ant command (and not the last output of the pipe).

How can I use that complex command with watch ?

C'estLaVie
  • 263
  • 1
  • 3
  • 9

1 Answers1

2

This works:

watch -n1 "netstat -ant | awk '{ print \$6}' | sort | uniq -c"

You're passing a double quoted string that happens to contain single quotes. Inside a double quoted string, $s meant as literal $s must be escaped ($6 => \$6). When you don't escape it, watch will likely receive

"netstat -ant | awk '{ print }' | sort | uniq -c"

(as $6 is likely to be unset), which would explain the output you're getting (awk '{ print }' in a pipeline is essentially a no-op, like cat).

Petr Skocik
  • 58,047
  • 6
  • 95
  • 142