Do While Loop Command help

Hello,
Can some one help me with do-while loop command and writing the out put to a file. Below is the command I want to use.

Command:

netstat -an |grep 1421 |grep "ESTABLISHED"  |wc -l

thanks in advance.

And the do-while loop should do what exactly?

Below command is displaying the needed output for me on the screen but i want the same out to be redirected to a file.

can you please help with this?

while [ 1 -eq 1 ]; do date; netstat -antp | grep 1424 | wc - l  ; sleep 1; done;
while true ; do (...) ; done >> outfile 

Prehaps you could condense you grepping into

grep -c 1421.*ESTABLISHED

does this look good?

while true ; do (date; netstat -antp | grep 1424 | grep -v "runmqlsr"| wc - l  ; sleep 1;) done >> test.out

---------- Post updated at 08:24 PM ---------- Previous update was at 08:18 PM ----------

this is working and writing to a file but how can i make it run in the background?

while true ; do (date; netstat -antp | grep 1424 | grep -v "runmqlsr"| wc - l  ; sleep 1;) done >> test.out

You actually don't need the parentheses. "(...)" was meant to be a generic placeholder for whatever command or pipe you would use. Looks OK otherwise.

its writing to a file but how can i make it run in the background? and not stop when i do ctrl+c on a shell?

( while true ; do date; netstat -antp | grep 1424 | grep -v "runmqlsr"| wc - l  ; sleep 1 ; done >> test.out ) &

PS: Please put CODE tags around your code. This facilitates copy & paste.

---------- Post updated at 08:47 PM ---------- Previous update was at 08:31 PM ----------

While thinking twice, I noted that '>' (without quotes) would be enough for re-directing the output. '>>' is not really needed.

I just executed below command and am not able to stop it.

can you please help me in how to stop it? ctrl+c is not working and i don't know how to find the PID for this process to kill it.

can you please let me know?

---------- Post updated at 08:58 PM ---------- Previous update was at 08:58 PM ----------

forgot to post the command:

while true ; do date; netstat -antp | grep 1424 | grep -v "runmqlsr"| wc - l  ; sleep 1; done & > test.out 2>&1&

Try:

kill %%

try this:

nohup scriptname.sh > filename &

it will run the script in the background and save the output in file "filename". Even if the terminal gets disconnected by any mean the script will not get terminated and will keep running in the background.

Cheers...:b:
Manu

I see no value in jamming this into a one-liner.

Also, if you're doing grep | grep | grep, save some trouble and put it into an awk. It can replace two greps and the wc -l.

#!/bin/sh

(       while true
        do
                date
                netstat -antp | awk '/1424/ && !/runmqlsr/ { C++ } END { print C}'
                sleep 1
        done ) </dev/null 2>/dev/null >>test.out & disown

If your shell doesn't have disown, it will work without it.