quit any time

how can i read input to quit any time, for instance "type q to quit"

I have a script like this

echo "The first choice"
read firstChoice
echo "The second choice"
read secondChoice

Looking for a code to quit any time by pressing q to quit

any help would be appreciated

thanks

something like this,

read var
if [ $var = "q" ]
then
echo "yes... quitting"
exit 1
fi

Like this

loop=1
while [ loop -eq "1" ]
   echo "Type F for first choice"
   read firstChoice
   echo "Type S for second choice"
   read secondChoice
   if [ "$firstChoice" = "F" ]; then
         echo "This is the first choice"
   elif [ "$secondChoice" = "S" ]; then
         echo "This is the second choice"
   else
         echo "no choice"
         loop=0
         exit 1   
   fi

  how do i catch the keyboard input for $var to quit any time? 
  if [ $var = "q" ]; then
      echo "yes... quitting"
    exit 1
   fi
done

you need to check the input each and every time

check() {
  if [ $1 = "q" ]; then
      echo "yes... quitting"
    exit 1
   fi
 }

loop=1
while [ loop -eq "1" ]
   echo "Type F for first choice"
   read firstChoice
   #
   check firstChoice
   #
   echo "Type S for second choice"
   read secondChoice
   #
   check secondChoice
   #
   if [ "$firstChoice" = "F" ]; then
         echo "This is the first choice"
   elif [ "$secondChoice" = "S" ]; then
         echo "This is the second choice"
   else
         echo "no choice"
         loop=0
         exit 1   
   fi
done

What do you mean by "any time"? Do you mean any time the script prompts you for input, or just whenever you press q? If you mean every time the script asks you for input, you could write a function like:

QUIT () {
            if [ $firstChoice = "q" ] || [ $secondChoice = "q" ]; then
              exit 1
            fi
             }

Then just call the function right after very read call. like:

loop=1
while [ loop -eq "1" ]
   echo "Type F for first choice"
   read firstChoice
QUIT
   echo "Type S for second choice"
   read secondChoice
QUIT
   if [ "$firstChoice" = "F" ]; then
         echo "This is the first choice"
   elif [ "$secondChoice" = "S" ]; then
         echo "This is the second choice"
   else
         echo "no choice"
         loop=0
         exit 1   
   fi

i mean whenever q is pressed

I've tried the Quit function that did it for me.

I thank you all for all your quick response