Just a quick note on addressing a common issue of running redundant commands when using shell loops. Let’s say you needed to get a count of network connections to an Oracle database server for each connection state: established, close_wait, etc. Here’s one way of doing this:

This works just fine, but there is an issue of performance, since you’re running netstat five times. Of course, you can run netstat once, redirect output to a temporary file and then parse the file.

Perhaps an even better option is to use an array to store output of netstat. Note the use of IFS to properly store array values that contain spaces. Also note the printf syntax to output array values one per line.


Leave A Reply

Please enter your comment!
Please enter your name here