LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-16-2011, 10:27 AM   #1
c_henry
LQ Newbie
 
Registered: Jan 2008
Posts: 6

Rep: Reputation: 0
Problems with getting or to work in a while loop


Hi,

I'm having trouble with my while loop, I can get it to work with one test, but not when I try to use an 'or'. Here's the script:

Code:
#!/bin/bash

echo -n "Enter a char [Q or q to exit]: "
read input

while [ "$input" != "Q" -o "$input" != "q" ]; do
    echo "You entered: $input"
    echo -n "Enter a char [Q or q to exit]: "
    read input
done
Here's the output when run:

Code:
$ ./while_loop.sh
Enter a char [Q or q to exit]: a
You entered: a
Enter a char [Q or q to exit]: Q
You entered: Q
Enter a char [Q or q to exit]: q
You entered: q
Enter a char [Q or q to exit]:
$
I've also tried:

Code:
#!/bin/bash

echo -n "Enter a char [Q or q to exit]: "
read input

while [[ $input != Q || $input != q ]]; do
    echo "You entered: $input"
    echo -n "Enter a char [Q or q to exit]: "
    read input
done
With exactly the same results. What am I missing (apart from talent)?

Many thanks,

c_henry
 
Old 08-16-2011, 10:35 AM   #2
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Hi,

try a logical AND:
Code:
while [ "$input" != "Q" -a "$input" != "q" ]; do
In your previous version you would enter a 'Q'. This is not equal to 'q' so the logical OR part evaluates to true and your loop still executes.
 
1 members found this post helpful.
Old 08-16-2011, 10:50 AM   #3
c_henry
LQ Newbie
 
Registered: Jan 2008
Posts: 6

Original Poster
Rep: Reputation: 0
Perfect! Really appreciate the quick reply.

Many thanks,

c_henry
 
Old 08-16-2011, 10:50 AM   #4
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
You're welcome.
 
Old 08-16-2011, 11:40 AM   #5
lej
LQ Newbie
 
Registered: Aug 2011
Location: UK
Distribution: Slackware
Posts: 29

Rep: Reputation: Disabled
On a side note, you may wish to use the prompt (-p) option to read, rather than a separate echo:

Code:
read -p "Enter a char [Q or q to exit]: " input
Also see the -N option, which will read the specified number of characters, so for example -N1 will return after reading a single character, which may be preferable in some cases (e.g. no need to press Return after typing 'q').
 
1 members found this post helpful.
Old 08-16-2011, 12:43 PM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
User-prompts are usually better handled this way:

Code:
while true; do

     read -p "Enter a char [Q or q to exit]: " input

     case $input in

          Q|q) echo "Goodbye!"
               break
               ;;
          *)   echo "You entered: $input"
               ;;

     esac
done
This will loop the prompt infinitely, until you enter Q/q, at which point the loop will break, and the script will continue.
 
1 members found this post helpful.
Old 08-16-2011, 01:27 PM   #7
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
If user input is the first thing to happen at the beginning of each loop one might even consider:
Code:
while read -p "Enter a char [Q or q to exit]: " input; do

     case $input in

          Q|q) echo "Goodbye!"
               break
               ;;
          *)   echo "You entered: $input"
               ;;

     esac
done
But this is a special case. David's solution is better suited for general purposes.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Serial loop back test doesnt work raypen Linux - Hardware 5 01-07-2012 06:06 AM
[SOLVED] imagemagick with for loop on the commandline does not work like expected markush Programming 2 05-26-2011 04:24 PM
[SOLVED] Having problems with Semaphores and infinite loop Uni616 Programming 4 10-05-2010 03:36 AM
Bash infinite loop problems? stp001 Programming 2 03-14-2010 02:41 PM
bash: ssh from within while loop doesn't work properly peter.lovell Programming 2 10-08-2008 07:44 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:56 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration