Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to ask more than one multiple choice question?

I would like to write a simple bash script for training multiple choice tests. Ask one question; give four choices (a, b, c, d); if user enters input, show if it is wrong or right and continue with the next question.

Here is my code so far:

#!/usr/bin/bash

echo Question1="How much is 2+2?"
echo a="1"
echo b="2"
echo c="3"
echo d="4"
read Question1

if [ "$Question1" = "d" ];
then
    echo "this is correct"
else
    echo "this is NOT correct"
fi

All samples about the read command example I found so far on youtube etc. stop after one question. How can I ask multiple questions? Entering another question does not work and bash shows a syntax error:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

#!/usr/bin/bash

echo Question1="How much is 2+2?"
echo a="1"
echo b="2"
echo c="3"
echo d="4"
read Question1

if [ "$Question1" = "d" ];
then
    echo "this is correct"
else
    echo "this is NOT correct"

echo Question2="How much is 2+1?"
echo a="1"
echo b="2"
echo c="3"
echo d="4"
read Question2

if [ "$Question2" = "c" ];
then
    echo "this is correct"
else
    echo "this is NOT correct"
fi

>Solution :

It is giving you a syntax error because you don’t have a fi after your first if statement, probably an error during copy/pasting.

Afterwards it should work fine.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading