Do while loop in a do while

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.

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

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.

This loop:

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.

2 Likes

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

 cp $TELNET_LOG /tmp/somelog

and no problem.

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

1 Like

I'll try the your suggestion.
Thanks. :slight_smile:

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.

 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:

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

wait $!

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

2>&1 &

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

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

jaysunn

1 Like

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

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