Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

while loop + case= infinity??

Posted on 2003-03-03
6
Medium Priority
?
182 Views
Last Modified: 2010-04-21
hello every1...i hope u all can help me with this code:

-------------------------------------------------------
echo -n "character:"
read char
while test -n $char
do
     case $char in
          a)echo "you typed a";;
          b)echo "you typed b";;
          c)echo "you typed c";;
          *)echo "you typed something else";;
     esac
done
-------------------------------------------------------

da prob is...da result will keep looping non-stop
how do i prevent dis infinite loop or is there another better method?

i'm using bash shell in mandrake linux 9.0 to generate da codes.

gildan2020
0
Comment
Question by:gildan2020
  • 2
  • 2
  • 2
6 Comments
 
LVL 3

Accepted Solution

by:
rfr1tz earned 150 total points
ID: 8057110
test is an old bourne shell construction. Probably better is shown below. Also, I moved the read statement inside the loop.

char='x'
while [[ -n $char ]]
  do
    echo -n "character:"
    read char
    case $char in
      a)echo "you typed a";;
      b)echo "you typed b";;
      c)echo "you typed c";;
      *)echo "you typed something else";;
    esac
  done
0
 
LVL 2

Expert Comment

by:jimbb
ID: 8059262
"test is an old bourne shell construction"

Nothing wrong with learning how to write scripts portably whenever possible.
0
 
LVL 3

Expert Comment

by:rfr1tz
ID: 8059335
It's up to you. I have never used "test" that I can remember. In additon, I would not have used [[ -n ... either
I would have used an if statement instead because it is more readable.
0
 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

 
LVL 2

Expert Comment

by:jimbb
ID: 8059379
Roger that, I usually use "if test -n" or "if [ -n". ;)
0
 

Author Comment

by:gildan2020
ID: 8062841
well, thanx for ur help, rfr1tz

i'll keep in mind of both ur comments :-)
0
 

Author Comment

by:gildan2020
ID: 8062842
i hav to alter the codes a bit in order for it to work with my
situation...anyway...it reli helps
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
Integration Management Part 2
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question