bash script question

Linux for blind general discussion blinux-list at redhat.com
Mon Dec 12 04:52:10 UTC 2022


First you have fi on the line right below do, but that fi seems to 
belong to your quiz function, so I moved it there. I also did not find 
done at the end of the do loop, so I have added that. Lastly the 
function was not closed, so I have added the closing curly bracket on a 
line by itself below the function. I also assume you want to actually 
exit the script if the response is not Y or y, so I add an exit after 
the echo. After these fixes, the script seems to work as expected, 
although I'm not sure you wanted to ask to start the quiz after an 
incorrect answer, but I left that as it was. I also separated the 
function from the main script by a blank line and fixed indentation, but 
this is just for readability, and your script will work as expected 
without these fixes. Find below the working script.


#!/bin/bash

quiz() {
   echo "Starting quiz..."
   echo "What is 2+2?"
   read response
   if [ "$response" == 4 ]
   then
     echo "You got it."
   else
     echo "That is incorrect."
   fi
}

until [ "$response" == 4 ]; do
   echo "Begin math quiz? (y/n)"
   read response
   if [ "$response" == "y" ] || [ "$response" == "Y" ]
   then
     SECONDS=0
     quiz
     echo "The quiz took you $SECONDS seconds to complete."
   else
     echo "Exiting..."
     exit
   fi
done



More information about the Blinux-list mailing list