# Do While Loop Command help

**URL:** <https://community.unix.com/t/do-while-loop-command-help/324069>\
**Category:** Shell Programming and Scripting\
**Created:** [February 8, 2013, 7:54pm UTC](https://community.unix.com/t/do-while-loop-command-help/324069 "2013-02-08T19:54:14Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![skrv](https://community.unix.com/letter_avatar/skrv/32/5_5575768a8748004e209b776fc1b2916d.png) [@skrv](https://community.unix.com/u/skrv)\
**Post date:** [February 8, 2013, 7:54pm UTC](https://community.unix.com/t/do-while-loop-command-help/324069/1 "2013-02-08T19:54:14Z")

</div>

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:

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

```

thanks in advance.

---

<div class="post-metadata">

**Author:** ![user8](https://community.unix.com/letter_avatar/user8/32/5_5575768a8748004e209b776fc1b2916d.png) [@user8](https://community.unix.com/u/user8)\
**Post date:** [February 8, 2013, 7:59pm UTC](https://community.unix.com/t/do-while-loop-command-help/324069/2 "2013-02-08T19:59:03Z")

</div>

And the do-while loop should do what exactly?

---

<div class="post-metadata">

**Author:** ![skrv](https://community.unix.com/letter_avatar/skrv/32/5_5575768a8748004e209b776fc1b2916d.png) [@skrv](https://community.unix.com/u/skrv)\
**Post date:** [February 8, 2013, 8:03pm UTC](https://community.unix.com/t/do-while-loop-command-help/324069/3 "2013-02-08T20:03:11Z")

</div>

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?

```nohighlight
while [1 -eq 1]; do date; netstat -antp | grep 1424 | wc - l ; sleep 1; done;

```

---

<div class="post-metadata">

**Author:** ![user8](https://community.unix.com/letter_avatar/user8/32/5_5575768a8748004e209b776fc1b2916d.png) [@user8](https://community.unix.com/u/user8)\
**Post date:** [February 8, 2013, 8:13pm UTC](https://community.unix.com/t/do-while-loop-command-help/324069/4 "2013-02-08T20:13:23Z")

</div>

```nohighlight
while true ; do (...) ; done >> outfile 

```

Prehaps you could condense you grepping into

```nohighlight
grep -c 1421.*ESTABLISHED

```

---

<div class="post-metadata">

**Author:** ![skrv](https://community.unix.com/letter_avatar/skrv/32/5_5575768a8748004e209b776fc1b2916d.png) [@skrv](https://community.unix.com/u/skrv)\
**Post date:** [February 8, 2013, 8:24pm UTC](https://community.unix.com/t/do-while-loop-command-help/324069/5 "2013-02-08T20:24:29Z")

</div>

does this look good?

```nohighlight
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?

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

```

---

<div class="post-metadata">

**Author:** ![user8](https://community.unix.com/letter_avatar/user8/32/5_5575768a8748004e209b776fc1b2916d.png) [@user8](https://community.unix.com/u/user8)\
**Post date:** [February 8, 2013, 8:25pm UTC](https://community.unix.com/t/do-while-loop-command-help/324069/6 "2013-02-08T20:25:30Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![skrv](https://community.unix.com/letter_avatar/skrv/32/5_5575768a8748004e209b776fc1b2916d.png) [@skrv](https://community.unix.com/u/skrv)\
**Post date:** [February 8, 2013, 8:29pm UTC](https://community.unix.com/t/do-while-loop-command-help/324069/7 "2013-02-08T20:29:49Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![user8](https://community.unix.com/letter_avatar/user8/32/5_5575768a8748004e209b776fc1b2916d.png) [@user8](https://community.unix.com/u/user8)\
**Post date:** [February 8, 2013, 8:47pm UTC](https://community.unix.com/t/do-while-loop-command-help/324069/8 "2013-02-08T20:47:10Z")

</div>

```nohighlight
( 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.

---

<div class="post-metadata">

**Author:** ![skrv](https://community.unix.com/letter_avatar/skrv/32/5_5575768a8748004e209b776fc1b2916d.png) [@skrv](https://community.unix.com/u/skrv)\
**Post date:** [February 8, 2013, 8:58pm UTC](https://community.unix.com/t/do-while-loop-command-help/324069/9 "2013-02-08T20:58:56Z")

</div>

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:

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

```

---

<div class="post-metadata">

**Author:** ![user8](https://community.unix.com/letter_avatar/user8/32/5_5575768a8748004e209b776fc1b2916d.png) [@user8](https://community.unix.com/u/user8)\
**Post date:** [February 8, 2013, 9:26pm UTC](https://community.unix.com/t/do-while-loop-command-help/324069/10 "2013-02-08T21:26:55Z")

</div>

Try:

```nohighlight
kill %%

```

---

<div class="post-metadata">

**Author:** ![Manoj\_Indalkar](https://community.unix.com/letter_avatar/manoj_indalkar/32/5_5575768a8748004e209b776fc1b2916d.png) [@Manoj\_Indalkar](https://community.unix.com/u/Manoj_Indalkar)\
**Post date:** [February 14, 2013, 6:41am UTC](https://community.unix.com/t/do-while-loop-command-help/324069/11 "2013-02-14T06:41:33Z")

</div>

> [@skrv](#):
>
> 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?

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

---

<div class="post-metadata">

**Author:** ![Corona688](https://community.unix.com/letter_avatar/corona688/32/5_5575768a8748004e209b776fc1b2916d.png) [@Corona688](https://community.unix.com/u/Corona688)\
**Post date:** [February 14, 2013, 12:25pm UTC](https://community.unix.com/t/do-while-loop-command-help/324069/12 "2013-02-14T12:25:25Z")

</div>

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.

```nohighlight
#!/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.
