?
Solved

Questionnaire bash script for Linux Shell

Posted on 2011-05-11
10
Medium Priority
?
1,128 Views
Last Modified: 2012-05-11
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

0
Comment
Question by:SME_Paddington
  • 3
  • 3
  • 3
9 Comments
 
LVL 38

Expert Comment

by:wesly_chen
ID: 35744081
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
 

Author Comment

by:SME_Paddington
ID: 35744134
Thank you for the correction wesly_chen, but I was ideally looking for an example too.

Kind regards.
0
 
LVL 8

Assisted Solution

by:point_pleasant
point_pleasant earned 1000 total points
ID: 35747060
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 8

Expert Comment

by:point_pleasant
ID: 35747465
ooops typo, the second sleep should be a lower case s


0
 

Author Comment

by:SME_Paddington
ID: 35748683
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
 
LVL 8

Expert Comment

by:point_pleasant
ID: 35749266
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
 
LVL 38

Accepted Solution

by:
wesly_chen earned 1000 total points
ID: 35758323
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
 
LVL 38

Expert Comment

by:wesly_chen
ID: 35758482
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
 

Author Closing Comment

by:SME_Paddington
ID: 35759510
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

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Fine Tune your automatic Updates for Ubuntu / Debian
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
Suggested Courses
Course of the Month13 days, 21 hours left to enroll

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question