Operating System - HP-UX
1753928 Members
9028 Online
108810 Solutions
New Discussion юеВ

stuck with run-on while-loop

 
SOLVED
Go to solution
Thi Vu
Frequent Advisor

stuck with run-on while-loop

Hello everyone,

I need help with the while-loop. I'd been at it for the last 2 days and I'm running out of steam and patient. This is my code:

while echo "Choose options: A) do this
B) do that
C) do whatever
X) exit"
read resp
do
if [ "$resp" = "A" ];
then
do this ...
fi

..... the same with other options

if [ "$resp" = "X" ];
then
exit 0
fi
done
scripting...
scripting...
....
echo "End of Script"

Here is my problem. When I tested the script if I hit ENTER or SPACE the script returns me to the begin of the loop asking to pick one of the option - GREAT. When I chose Option A the script goes into that section and perform its job - when the job is done the script is then jump BACK to the top of the while-loop. It does not exit the while-loop and continue on with the script but just back to the while-loop. It will only exit if I choose "X" but then the rest of the script is not run. I had tried counter in the while-loop (i.e:
count=0
while [ "$resp" != "X" ] || [ "$count" = "0" ];
(this does not recogize by sh i.e shell only interpret : [ A != "X" .......-blank )
while [ "$resp" != "X" OR "$count" = "0" ]
do
....
...
count=1 (if job success)
count=0 (if job not success)
done
I tested this with "set -x" option to see how the shell interpret my command. When I ran the script the while-loop looks like this
while [ A != "X" OR 0 = 0 ]
do
......
count=1 (job is success)
AND the script goes back to the top of while-loop again (the shell show :
while [ != "X" OR 1 = 0 ]
do
.....
it waits here for me to choose X to exit or another option

From the above I see that count return a value of 1 thus the while-loop should exit BUT it does not. Please help, my patient tank is running pretty low. Thank you in advance.
Thi
8 REPLIES 8
James R. Ferguson
Acclaimed Contributor
Solution

Re: stuck with run-on while-loop

Hi:

'break' will break-out of the loop. 'continue' will start the loop again.

...JRF...
Mark Vollmers
Esteemed Contributor

Re: stuck with run-on while-loop

James is right about the break and continue commands to quit the loop. I'm a little confused about how you have it set up, though. It sounds like you want to select A, for example, and then quit the loop and run some other stuff, or B and then run the same stuff, etc. X will end the script. Why not use a 'case' statement instead, since this sounds like a normal menu? You wouldn't have to worry about the looping. Just a thought.

Mark
"We apologize for the inconvience" -God's last message to all creation, from Douglas Adams "So Long and Thanks for all the Fish"
James R. Ferguson
Acclaimed Contributor

Re: stuck with run-on while-loop

Hi again:

See if this example helps you:

#!/usr/bin/sh
while true
do
echo "...enter choice..."
read CHOICE
case $CHOICE in
0 )
echo "> doing 0 <"
break
;;
1 )
echo "> doing 1 <"
continue
;;
* )
echo "> doing something else <"
;;
esac
done
echo "...continuing after loop..."
exit 0

...JRF...
Jim Moffitt_1
Valued Contributor

Re: stuck with run-on while-loop

Here's one tip, do not use the ; after the if statement, that might be causing the loop.
Mark Fenton
Esteemed Contributor

Re: stuck with run-on while-loop

while echo "Choose options: A) do this
B) do that
C) do whatever
X) exit"
read resp
do
if [ "$resp" = "A" ];
then
do this ...
fi
Agree with the above -- case would be preferable to if.
Also -- that "do" after read resp is really a "done", no?
Jim Moffitt_1
Valued Contributor

Re: stuck with run-on while-loop

I think the done should be after fi
Mark Vollmers
Esteemed Contributor

Re: stuck with run-on while-loop

Mark-

The done is later on, after the last do option. Switching the first do to a done would end the loop after getting the variable, I think. It would make the while kinda useless.
"We apologize for the inconvience" -God's last message to all creation, from Douglas Adams "So Long and Thanks for all the Fish"
Thi Vu
Frequent Advisor

Re: stuck with run-on while-loop

Thank you for all of the responses. I did try the CASE for my problem above but it did not do what I wanted it to do (i.e: if user hit enter by mistake the script EXIT with the CASE - I wanted the script to remain at the menu until he/she choses X to exit). The "break" works great for me. Again, thank you everyone for your help.

Thi