Search code examples
bashloopsinfinite

Why is this Infinite Looping - Simple Game script in Bash


Writing a simple game script to guess the correct number of 42.

I get an infinite loop in the for/else statements. Can someone please tell me why? I am very very new to Bash

Thanks

    #!/bin/bash

read -p "Please pick a number between 1 and 100: " x
q=exit
while true 
do
if [ "$x" = "q" ]
    then
        exit
      

fi
#done
if  [ "$x" -gt "42" ]; then 
echo -e "That number is too high!" 
else read -p "try again :" 

fi
if [ "$x" -lt "42" ]; 
then echo -e "That number is too low!" 
else read -p "try again :"
fi
if [ "$x" = "42" ] ; 
then echo -e "You are Correct!!!" 
else read -p "try again :"
fi
done
exit

Solution

  • This line populates $x:

    read -p "Please pick a number between 1 and 100: " x
    

    This line populates $REPLY:

    read -p "try again :"