Multiple choice quiz im shell not working


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Multiple choice quiz im shell not working
# 1  
Old 01-23-2019
Multiple choice quiz im shell not working

Hi, I have to create a quiz im form of bash-shell-script.
The problem is: I don't find a way to bring multiple choice questions to work. As long as someone selects only one of the possible answers everything is fine, but if one selects two or more options it doesn't work.
The code I used is below, can someone help me?
P.S.: Frage means question, loesung means solution.

Code:
#!/bin/bash
FRAGE7=`echo "$QUERY_STRING" | sed -n 's/^.frage7=\([^&]\).*$/\1/p' | sed "s/%20/ /g"`

if [ "$FRAGE5" == "male" ]; then
        LOESUNG="wrong!"
elif [ "$FRAGE5" == "female" ]; then
        LOESUNG="right!"
elif [ "$FRAGE5" == "other" ]; then
        LOESUNG="wrong!"
elif [ "$FRAGE5" == "fortlaufend" ]; then
        LOESUNG="wrong!"
elif [ "$FRAGE5" == "fotos" ]; then
        LOESUNG="right!"
elif [ "$FRAGE5" == "landkarten" ]; then
        LOESUNG="wrong!"
elif [ "$FRAGE5" == "musikalien" ]; then
        LOESUNG="wrong!"
fi

echo "Content-type: text/html"
echo ""
echo "<html><head><title>Lösung</title><meta charset="UTF-8"></head>"
echo "<body>"
echo "<h3>Frage 7</h3>"
echo "<p>Your answer is <b>$LOESUNG</b></p>"
echo "</body></html>"

# 2  
Old 01-23-2019
Welcome to the forum.


Is that quiz homework / classwork? Then pls. reopen the request in the https://www.unix.com/homework-and-coursework-questions/ forum. You may want to add some more details like sample input data, and some evaluation logics for e.g. multiple answers, and their formatting. And, consider using the case ... esac for the flow control in lieu of that many if ... elif ... fi commands.

Last edited by RudiC; 01-23-2019 at 07:00 AM..
# 3  
Old 01-23-2019
First, you are using the varable FRAGE7 in the first line of your code and FRAGE5 in the if construct.

Second, why all the if-then-elif statements?

Code:
case "$FRAGE7" in
   female|fotos) LOESUNG="right!" ;;
   *) LOESUNG="wrong!" ;;
esac

or
Code:
case "$FRAGE7" in
   female) LOESUNG="right!" ;;
   fotos) LOESUNG="right!" ;;
   *) LOESUNG="wrong!" ;;
esac

Third, you don't need that ugly echo ... | sed construct which by the look of it will return one character which is not an ampersand (&).

Code:
FRAGE7=${FRAGE7#frage7=}

will get rid of the prefix and
Code:
FRAGE7=${FRAGE7//%20/ }

should convert the %20 to spaces.

Andrew
# 4  
Old 01-23-2019
Hi RudiC, thank you for the suggestion. Yes, it's something like a classwork. Should I delete the post in here when I reopen it in the the other part of the forum? Sadly I don't have those information about the evaluation logics, we had to learn the code alone without any basis and without details about the evaluation mode. I'll try the case...esac, thank you very much.
# 5  
Old 01-23-2019
I've closed it for you. When reopening as homework, pls. fill in the entire form, and add as much info as possible, like data, data formats (like "how do you present multiple choice answers"), YOUR thoughts on what should happen. That helps us help you.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Multiple expect/send statements not working in shell script

Hi I am trying the following in my bash script which logs into my machine and runs a command. Trying to solve this using expect. The first expect statement is hit and it enters the address "10.10.0.10" but when the second expect statement is hit it exits #!/bin/bash expect -c ' spawn... (2 Replies)
Discussion started by: skorada
2 Replies

2. Shell Programming and Scripting

Cp not working in shell script but running perfectly from shell

Dear All, I have script. Dest="" IFS=' ' for translation in $(echo $MY_MAP) do t1=$(echo $translation | cut -d"=" -f1) t2=$(echo $translation | cut -d"=" -f2| cut -d"," -f1) if then Dest=$UNX/$u_product_path/$u_study_path/$UNXTR/$t2 break; ... (4 Replies)
Discussion started by: yadavricky
4 Replies

3. Filesystems, Disks and Memory

SSD Caching, how its done, right choice?

Hello, someone needed VPS with SSD caching, he want to use server for websites hosting. What does that mean, this SSD caching and is it optimal solution for this? Also i listen some SSD dont like too much of writting so how one can recognise certain SSD is made the way that its not destroyed... (1 Reply)
Discussion started by: postcd
1 Replies

4. Shell Programming and Scripting

working with multiple columns

Hi, I have a file with 270 column. 5.5 8.8 9.6 5.4...............9.9 7.0 3.4 . . . . . 8.8 5.7 6.3 4.1 3.8 9.9...........5.5 6.4 3.2 The last two values are my mean and std. I want for every cell content subtract it with the mean and divide by std and output the values example... (1 Reply)
Discussion started by: Diya123
1 Replies

5. Shell Programming and Scripting

AWK quiz, multiple choice

Nobody cared about the original post, or perhaps it was a bit difficult to guess. Therefore I am including a poll here. nawk -F'+' ' {i=NF;for(;i;i--)if($i)w++} END{ for(k in w){c=w;x=xk",";if(c>m)m=c} for(;m;m--)if(m in x)print m,x } ' <(man nawk) (6 Replies)
Discussion started by: colemar
6 Replies

6. Shell Programming and Scripting

Shell quiz: implement a mutex

How would you make sure that a shell script (or a portion thereof) does not run (it waits or it terminates with error) when another instance of it is already running? (11 Replies)
Discussion started by: colemar
11 Replies

7. Shell Programming and Scripting

Shell quiz: emulate an associative array

Most shells flavors do not have associative arrays a.k.a. maps. How would you emulate an associative array? I had this problem once and found a working solution, but I don't want to spoil the game hence I wont tell it. Wonder if anyone comes up with something better. (5 Replies)
Discussion started by: colemar
5 Replies

8. UNIX Desktop Questions & Answers

Window Manager of the ... Choice

Inspired by Window Manager of the Year threads from LinuxQuestions.org Like these: 2002 | 2003 | 2004 | 2005 | 2006 I wonder what WMs are used by UNIX people ... People sometimes select diffrent WMs for (old and slow) laptop and (overpowered) workstation, that is why poll allows multiple... (8 Replies)
Discussion started by: vermaden
8 Replies

9. Shell Programming and Scripting

how to write examination(multiple choice) script in unix

Hi, I am unable to get idea, how to write the script like multiple choice exam model script , how to specify the welcome to the exam(name) and if he select option then it has to enter into the exam , then choosing the answers,next question and going to previous question. Thanks & Regards ... (0 Replies)
Discussion started by: palreddy7
0 Replies
Login or Register to Ask a Question