- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- stuck with run-on while-loop
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-27-2001 08:24 AM
тАО04-27-2001 08:24 AM
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
Solved! Go to Solution.
- Tags:
- while loop
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-27-2001 08:35 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-27-2001 08:48 AM
тАО04-27-2001 08:48 AM
Re: stuck with run-on while-loop
Mark
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-27-2001 08:54 AM
тАО04-27-2001 08:54 AM
Re: stuck with run-on while-loop
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-27-2001 09:25 AM
тАО04-27-2001 09:25 AM
Re: stuck with run-on while-loop
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-27-2001 09:46 AM
тАО04-27-2001 09:46 AM
Re: stuck with run-on while-loop
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-27-2001 09:55 AM
тАО04-27-2001 09:55 AM
Re: stuck with run-on while-loop
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-27-2001 10:20 AM
тАО04-27-2001 10:20 AM
Re: stuck with run-on while-loop
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-30-2001 07:33 AM
тАО04-30-2001 07:33 AM
Re: stuck with run-on while-loop
Thi