LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Problems with getting or to work in a while loop (https://www.linuxquestions.org/questions/programming-9/problems-with-getting-or-to-work-in-a-while-loop-897621/)

c_henry 08-16-2011 10:27 AM

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

crts 08-16-2011 10:35 AM

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.

c_henry 08-16-2011 10:50 AM

Perfect! Really appreciate the quick reply.

Many thanks,

c_henry

crts 08-16-2011 10:50 AM

You're welcome.

lej 08-16-2011 11:40 AM

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').

David the H. 08-16-2011 12:43 PM

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.

crts 08-16-2011 01:27 PM

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.


All times are GMT -5. The time now is 12:21 AM.