Help with infinite loop problem

  1. The problem statement, all variables and given/known data:
    My problem is an infinite loop when i press any other key other then Y or y in the while loop. what i want it to do is return to the normal script outside of it if pressing N or n or keep asking the same question if its any other button. Perhaps my knowledge and understanding of loops is inaccurate. But i have made it very similar to the lecture shown ones and it isnt working out very well. Thanks in advance.

  2. Relevant commands, code, scripts, algorithms:

P.S. I am using cygwin in windows to do this script.

  1. The attempts at a solution (include all code and scripts):
"4")
clear
echo Would you like to exit? [y/n]?
read answer
while [ "$answer" != "Y" ] || [ "$answer" != "y" ]; do
        case "$answer" in
        "Y" | "y")  clear; exit ;;
        "N" | "n")
                echo Press enter to continue
                read a ;;
        *)
                echo Invalid input
                echo Press enter to continue...
                read a
                ;;
        esac
        if [ $answer != "Y" ] || [ "$answer" != "y" ]; then
        echo "Press enter to continue..."
        read A
        fi




done
;;

  1. Insearch: (University of Technology Sydney), Sydney NSW, Australia, James Hu (web systems), www(dot)insearch.edu.au/default.aspx?ArticleID=430

Note: Without school/professor/course information, you will be banned if you post here! You must complete the entire template (not just parts of it).

There's no point checking the same thing 3 times, check it once, and do what you need to do there, once.

while true
do
        echo "Would you like to quit?  [Y/N] "

        read ANSWER

        case "$ANSWER" in
        [yY])
                break
                ;;
        [nN])
                ;;
        *)     echo "Invalid response"
                ;;
        esac

done
1 Like

Thanks for the prompt reply. But could you tell me what you mean by saying i was testing the same thing 3 times ><? Just wanna learn more.

Means exactly what it says. You're checking the value of $answer in 3 places, once is enough.

while [ "$answer" != "Y" ] || [ "$answer" != "y" ]; do

...

        case "$answer" in
        "Y" | "y")  clear; exit ;;
        "N" | "n")

...

        if [ $answer != "Y" ] || [ "$answer" != "y" ]; then

...

ok let i telling you that's problem occurred when we put the value of i++ for the set time and when i++time divided by 0 then given exp-ration always infinite and while loop always depend on two way condition true or wrong and similar with if else statement way same but condition not similar in both statement for more help if you want related to solve expression