Questionnaire bash script for Linux Shell

Dear Experts,

Could you please help me write a questionnaire script, to help test some of our new prospective interns. I will need an end of script echo, as to the result obtained - e.g. how many correct, and incorrect answers; points will be given for percentages too. The script would hopefully take the questions and answers from two separate files, such as: Questions.txt and Answers.txt.

Kind regards, SME_Paddington

My questions, and answers for the two files so far are:
Questions.txt:
1. How many Mb's per second can be transferred over Ethernet?
2. What does FTP stand for?
3. What does IDS stand for?
4. How many bits are there in a byte?
5. What is the 'echo' equivalent in C++?
6. What is the TCP three way hand shake in terms of packets?

Answers.txt:
1. 100mb
2. File Transfer Protocol
3. Intrusion Detection System
4. 8
5. cout
6. SYN SYN-ACK SYN-ACK-ACK

Open in new window

SME_PaddingtonAsked:
Who is Participating?
 
wesly_chenConnect With a Mentor Commented:
Hi SME_Paddington,
point_pleasant's second post is written in shell script.
http://www.experts-exchange.com/Programming/System/Linux/Q_27033471.html#a35749266

Let me give you the Questions.txt and Answers.txt in the selection format.
I just have some modification to separate the questions and answers into two files.

Put this three files in the same directory and
chmod +x QandAtest.sh
./QandAtest.sh
Questions.txt:
1; How many Mb's per second can be transferred over Ethernet?;(a) 1mb;(b) 10mb;(c) 100mb;(d) 1000mb
2; What does FTP stand for?;(a) Fast Transfer Protocol;(b) File Transfer Process;(c) File Transfer Protocol;(d) Font Type Pointer
3; What does IDS stand for?;(a) IDentification System;(b) Internet Defending System;(c) Intrusion Detection System;(d) Index Database System
4; How many bits are there in a byte?;(a) 8;(b) 10;(c) 16;(d) 4
5; What is the 'echo' equivalent in C++?;(a) print;(b) cout;(c) printf;(d) echo

Open in new window

QandAtest.sh:
-----------
#!/bin/bash

tput clear
QUESTION_FILE='./Questions.txt'
ANSWER_FILE='./Answers.txt'
num_questions=0
num_correct=0
echo -n "Enter your name: "
read name

while read line
do
        ((num_questions++))
        question_number=`echo $line | cut -f1 -d';'`
        question=`echo $line | cut -f2 -d';'`
        option_a=`echo $line | cut -f3 -d';'`
        option_b=`echo $line | cut -f4 -d';'`
        option_c=`echo $line | cut -f5 -d';'`
        option_d=`echo $line | cut -f6 -d';'`
        correct_answer=`grep ^${question_number} $ANSWER_FILE | cut -f2 -d';'`
        tput clear
        echo "Question number $question_number"
        echo "$question "
        echo "$option_a "
        echo "$option_b "
        echo "$option_c "
        echo "$option_d "
        read -p 'Input Selection: ' input_answer < /dev/tty
        if [ "$input_answer" == "$correct_answer" ]
        then
                ((num_correct++))
        fi
        sleep 1
done < $QUESTION_FILE
echo "Congratulations $name you answered $num_correct questions out of $num_questions"

Open in new window

Answers.txt:
1;b
2;c
3;c
4;a
5;b

Open in new window

0
 
wesly_chenCommented:
Your questions should be in "selection" or multiple choice questions which will be easier for script to parse.
i.e.
Q1. How many Mb's per second can be transferred over Ethernet?
 (a) 1mb
 (b) 10mb
 (c) 100mb
 (d) 1000mb

If I type 100MB or 100 MB or 100, which should match your answer sheet but hard for script to parse it.
So make your question in selection will be no error prompt....

By the way, the answer should be (b), because 100mb is Fast Ethernet.....
0
 
SME_PaddingtonAuthor Commented:
Thank you for the correction wesly_chen, but I was ideally looking for an example too.

Kind regards.
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
point_pleasantConnect With a Mentor Commented:
give this a whirl,

the system call for (tput clear) may not be portable to all unix distros

Usage perl ./questions.pl

Q&A file lay out looks like this:
question#,question,answer a, answer b, answerc, correct answer
1,What is the square root of 9,1,2,3,9,d
2,What is the square root of 9,1,2,3,9,d
3,What is the square root of 9,1,2,3,9,d
4,What is the square root of 9,1,2,3,9,d
5,What is the square root of 9,1,2,3,9,d
6,What is the square root of 9,1,2,3,9,d
7,What is the square root of 9,1,2,3,9,d
8,What is the square root of 9,1,2,3,9,d
9,What is the square root of 9,1,2,3,9,d

code is here

open (DATA_FILE, "/tmp/QandAfile.txt") or die "Can't open file";
$question_no=0;
$correct_no=0;
print "Please Enter Your Name: ";$name=<STDIN>;
while ($question_line=<DATA_FILE>) {
        $question_no = $question_no + 1;
        @parsed_question=split(/\,/,$question_line);
        $question_no=$parsed_question[0];
        $question=$parsed_question[1];
        $answer_a=$parsed_question[2];
        $answer_b=$parsed_question[3];
        $answer_c=$parsed_question[4];
        $answer_d=$parsed_question[5];
        $correct_answer=$parsed_question[6];
        START:
        system("tput clear") == 0 or die "screen clear failed";
        print "Question number $question_no\n";
        print "$question ?\n";
        print "a) $answer_a \n";
        print "b) $answer_b \n";
        print "c) $answer_c \n";
        print "d) $answer_d \n";
        print "Enter selection: ";$input_answer=<STDIN>;
        chomp($input_answer);
        chomp($correct_answer);
        chomp($$name);
        if ($input_answer ne "a" && $input_answer ne "b" && $input_answer ne "c" && $input_answer ne "d") {
                print "Please choose a,b,c, or d\n";
                print $input_answer;
                sleep(2);
                goto START;
        }
        if ($input_answer eq $correct_answer) {
                $correct_no = $correct_no + 1;
        }
        Sleep(2);
}
print "Congratulations $name you scored $correct_no correct out of $question_no\n"

0
 
point_pleasantCommented:
ooops typo, the second sleep should be a lower case s


0
 
SME_PaddingtonAuthor Commented:
Dear Experts,

I will try your code point_pleasant, but I still need bash.

Is there any way to write this in shell script?

Kind Regards
0
 
point_pleasantCommented:
give this a shot same file format for Q&Q as before


exec 4</tmp/QandAfile.txt
tput clear
num_questions=0;
num_correct=0;
echo -n "Enter your name: ";
read name;

while read line
do
        num_questions=`expr $num_questions + 1`;
        question_number=`echo $line | cut -f1 -d','`;
        question=`echo $line | cut -f2 -d','`;
        answer_a=`echo $line | cut -f3 -d','`;
        answer_b=`echo $line | cut -f4 -d','`;
        answer_c=`echo $line | cut -f5 -d','`;
        answer_d=`echo $line | cut -f6 -d','`;
        correct_answer=`echo $line | cut -f7 -d','`;
        tput clear;
        echo "Question number $question_number";
        echo "$question ?";
        echo "a) $answer_a ";
        echo "b) $answer_b ";
        echo "c) $answer_c ";
        echo "d) $answer_d ";
        read -p 'Input Selection: ' input_answer < /dev/tty;
        if [ "$input_answer" == "$correct_answer" ]
        then
                num_correct=`expr $num_correct + 1`;
        fi
        sleep 2;
done < /tmp/QandAfile.txt
echo "Congratulations $name you answered $num_correct questions out of $num_questions"
0
 
wesly_chenCommented:
I verify point_pleasant's  shell script at
http://www.experts-exchange.com/Programming/System/Linux/Q_27033471.html#a35749266

with this QandAfile.txt and it works.

I just did some modification based on point_pleasant's script which I don't want the answers in the question file.
1, How many Mb's per second can be transferred over Ethernet?,1mb,10mb,100mb,1000mb,b
2, What does FTP stand for?,Fast Transfer Protocol,File Transfer Process,File Transfer Protocol,Font Type Pointer,c
3, What does IDS stand for?,IDentification System,Internet Defending System,Intrusion Detection System,Index Database System,c
4, How many bits are there in a byte?,8,10,16,4,a
5, What is the 'echo' equivalent in C++?,print,cout,printf,reflect,b

Open in new window

0
 
SME_PaddingtonAuthor Commented:
Dear Experts,

Wesly_chen's solution provides the answer.

I initially required this coding example for two days ago, but it has been moved to this Monday.
I believed that the implementation would not have been properly performed by Monday, as this is when it is required; and thus I did not want to keep open, a question that was essentially (previously) going no-where.

Also, Wesley_chen - excellent implementation, I am very impressed. I believe that multiple solutions should be accepted. Such as post ID: 35758323 and 35747060

As 35758323 is a modification and revision of 35747060.

Kind regards, SME_Paddington
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.