# Do while loop in a do while

**URL:** <https://community.unix.com/t/do-while-loop-in-a-do-while/299365>\
**Category:** Shell Programming and Scripting\
**Created:** [November 16, 2011, 5:29pm UTC](https://community.unix.com/t/do-while-loop-in-a-do-while/299365 "2011-11-16T17:29:12Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![nitin](https://community.unix.com/letter_avatar/nitin/32/6_449c533f586939b6e5f246632a46421f.png) [@nitin](https://community.unix.com/u/nitin)\
**Post date:** [November 16, 2011, 5:29pm UTC](https://community.unix.com/t/do-while-loop-in-a-do-while/299365/1 "2011-11-16T17:29:12Z")

</div>

Hi,  
I've a script that is using do while loop. It reads some some stuf using ps command then calls a function. The function has another do while loop in it.  
My problem is that when the second loop is completed, the script exits. The whole thing is a bit complex, I'll post some snippet here. Any suggestions will be appreciated like always.

```nohighlight
TELNET_QUIT=/tmp/telnet_quit.tmp # file for telnet sessions. Suppress on screen display
TELNET_LOG=/tmp/telnet_`date +%Y-%m-%d`.tmp
TELNET_TEST=NULL
echo "QUIT">$TELNET_QUIT

function telnetTest {
 	echo " Please wait. "
	while read -r -u3 LINE1; read -r -u4 LINE2;
		do
		telnet $LINE1 $LINE2 <$TELNET_QUIT >$TELNET_LOG 2>&1
		wait $!
		TELNET_TEST=$(grep "Connection closed by foreign host." $TELNET_LOG | wc -l)
		if (($TELNET_TEST)); then
		   echo " OK. $LINE2" >>/tmp/somelog
		else
		   echo " Problem! $LINE2" >>/tmp/somelog
		fi
		sleep 4
		more $TELNET_LOG >>/tmp/somelog
	done 3< "$TMPFILE" 4< "$TMPFILE2" # Works till here, then exits out completely 
} # End of function

function getmyInfo {
# We get my_SERVERS_NUM value from some where� snip 
my_SERVERS_NUM=1 # hard coding for one only
my_SERVER=10.10.10.10
TMPFILE=/tmp/tmp1.out
echo 10.10.10.10>$TMPFILE
my_SERVER_PORT=9120
TMPFILE2=/tmp/tmp2.out
echo 9120 >$TMPFILE2

if [$my_SERVERS_NUM -ge 2]; then
	echo " Many servers to check. " 
	telnetTest
else
	echo " Only one server to check. "
	telnetTest
fi
} # End of function

ps -ef | awk '/[m]yAgent/{print $8}' | while read lines ;
	do
    		set -- $lines
		my_PATH=`echo $lines | sed '$s/myAgent.bin$//'`
		echo -n " Analyze this instance (y/n)? " 
		read answer</dev/tty
		if ["$answer" = y] ; then	
		my_pid=`ps -ef | grep $line | grep -v grep | awk '{print $2}'`
		getmyInfo
		fi
	done

#some cleanup stuff
#EOF

```

Thanks for looking.

Nitin

---

<div class="post-metadata">

**Author:** ![Chubler\_XL](https://community.unix.com/user_avatar/community.unix.com/chubler_xl/32/2077_2.png) [@Chubler\_XL](https://community.unix.com/u/Chubler_XL)\
**Post date:** [November 16, 2011, 9:36pm UTC](https://community.unix.com/t/do-while-loop-in-a-do-while/299365/2 "2011-11-16T21:36:12Z")

</div>

It's probably the more command it reads stdin - try using cat instead.

---

<div class="post-metadata">

**Author:** ![agama](https://community.unix.com/user_avatar/community.unix.com/agama/32/21613_2.png) [@agama](https://community.unix.com/u/agama)\
**Post date:** [November 16, 2011, 9:39pm UTC](https://community.unix.com/t/do-while-loop-in-a-do-while/299365/3 "2011-11-16T21:39:56Z")

</div>

> [@chubler\_xl](#):
>
> It's probably the more command it reads stdin - try using cat instead.

I thought so too, but the reads on the while are reading from fd3 and fd4 and not stdin. I could be wrong, but I did pull down the code I couldn't get it to break.

Maybe adding `set -x` at the top of the script with the hope that the trace info might provide a clue as to what is happening.

Might also help to know which shell and operating system.

---

<div class="post-metadata">

**Author:** ![Chubler\_XL](https://community.unix.com/user_avatar/community.unix.com/chubler_xl/32/2077_2.png) [@Chubler\_XL](https://community.unix.com/u/Chubler_XL)\
**Post date:** [November 16, 2011, 9:55pm UTC](https://community.unix.com/t/do-while-loop-in-a-do-while/299365/4 "2011-11-16T21:55:53Z")

</div>

This loop:

```nohighlight
ps -ef | awk '/[m]yAgent/{print $8}' | while read lines ;

```

is using stdin so anything called from within it (like telnetTest()) cannot use any process that opens (and closes) stdin.

---

<div class="post-metadata">

**Author:** ![agama](https://community.unix.com/user_avatar/community.unix.com/agama/32/21613_2.png) [@agama](https://community.unix.com/u/agama)\
**Post date:** [November 16, 2011, 10:24pm UTC](https://community.unix.com/t/do-while-loop-in-a-do-while/299365/5 "2011-11-16T22:24:01Z")

</div>

I feel stupid -- I had set all of the tmp files to a different directory which was failing and my test never got to the more. Grrrrrr. Fixed my bug and problem reproduced, changed more to

```nohighlight
 cp $TELNET_LOG /tmp/somelog

```

and no problem.

@Chubler_XL -- thanks for making me look harder at this. Hope OP sees it now.

---

<div class="post-metadata">

**Author:** ![nitin](https://community.unix.com/letter_avatar/nitin/32/6_449c533f586939b6e5f246632a46421f.png) [@nitin](https://community.unix.com/u/nitin)\
**Post date:** [November 17, 2011, 9:40am UTC](https://community.unix.com/t/do-while-loop-in-a-do-while/299365/6 "2011-11-17T09:40:02Z")

</div>

I'll try the your suggestion.  
Thanks. 🙂

---

<div class="post-metadata">

**Author:** ![nitin](https://community.unix.com/letter_avatar/nitin/32/6_449c533f586939b6e5f246632a46421f.png) [@nitin](https://community.unix.com/u/nitin)\
**Post date:** [November 18, 2011, 12:55pm UTC](https://community.unix.com/t/do-while-loop-in-a-do-while/299365/7 "2011-11-18T12:55:37Z")

</div>

Thanks to all, you guys are great. The cat command seem to have worked. I could not use cp, as I was pushing lots of output from other areas to the log. Here is the snippet of my telnet function.

```nohighlight
 function telnetTest {
 	echo " Please wait. "
	while read -r -u3 LINE1; read -r -u4 LINE2;
		do
		telnet $LINE1 $LINE2 <$TELNET_QUIT >$TELNET_LOG 2>&1
		wait $!
		TELNET_TEST=$(grep "Connection closed by foreign host." $TELNET_LOG | wc -l)
		if (($TELNET_TEST)); then
		   echo " OK. $LINE2" >>/tmp/somelog
		else
		   echo " Problem! $LINE2" >>/tmp/somelog
		fi
		sleep 4
		cat $TELNET_LOG >>/tmp/somelog
	done 3< "$TMPFILE" 4< "$TMPFILE2"  
} # End of function

```

:b:

---

<div class="post-metadata">

**Author:** ![jaysunn](https://community.unix.com/user_avatar/community.unix.com/jaysunn/32/1476_2.png) [@jaysunn](https://community.unix.com/u/jaysunn)\
**Post date:** [November 22, 2011, 3:50pm UTC](https://community.unix.com/t/do-while-loop-in-a-do-while/299365/8 "2011-11-22T15:50:24Z")

</div>

Sorry to jump in, and a bit off topic.  
However I always thought that in order to use the wait $! syntax below:

```nohighlight
wait $!

```

You would have to put the command into the background using the ampersand

```nohighlight
2>&1 &

```

.  
Does sending the stdout and stderr to a file constitute the proper usage for wait?

```nohighlight
telnet $LINE1 $LINE2 <$TELNET_QUIT >$TELNET_LOG 2>&1
		wait $!
		TELNET_TEST 

```

jaysunn

---

<div class="post-metadata">

**Author:** ![agama](https://community.unix.com/user_avatar/community.unix.com/agama/32/21613_2.png) [@agama](https://community.unix.com/u/agama)\
**Post date:** [November 22, 2011, 6:02pm UTC](https://community.unix.com/t/do-while-loop-in-a-do-while/299365/9 "2011-11-22T18:02:27Z")

</div>

> [@jaysunn](#):
>
> Sorry to jump in, and a bit off topic.  
> However I always thought that in order to use the wait $! syntax below:
> 
> ```plaintext
> wait $!
> 
> ```
> 
> You would have to put the command into the background using the ampersand
> 
> ```plaintext
> 2>&1 &
> 
> ```
> 
> .  
> Does sending the stdout and stderr to a file constitute the proper usage for wait?
> 
> ```plaintext
> telnet $LINE1 $LINE2 <$TELNET_QUIT >$TELNET_LOG 2>&1
> wait $!
> TELNET_TEST 
> 
> ```
> 
> jaysunn

Hadn't noticed that before, but you are right; there'd be nothing to wait on since nothing was run asynchronously.

---

<div class="post-metadata">

**Author:** ![nitin](https://community.unix.com/letter_avatar/nitin/32/6_449c533f586939b6e5f246632a46421f.png) [@nitin](https://community.unix.com/u/nitin)\
**Post date:** [November 23, 2011, 2:25am UTC](https://community.unix.com/t/do-while-loop-in-a-do-while/299365/10 "2011-11-23T02:25:51Z")

</div>

I think I'll try the &. Good catch.
