Print questions from a questions folder in a sequential order

1.) I am to write scripts that will be phasetest folder in the home directory.
2.) The folder should have a set-up,phase and display files

I have written a small script which i used to check for the existing users and their password.

What I need help with:
I have a set of questions in a file named questions,i want to be able to display questions one after the order,enter and answer and store my answer in a new file called results and be able to check if it is correct.I have questions 1 to 8.
question example:
9. How would you create a new directory called docs?
a) makedocs
b) mkdir docs
c) create docs
d) none of these
Enter answer
b
Enter question number, N for next question, P for previous question or Q for done
n

Thanks.

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted!

  1. The problem statement, all variables and given/known data:
    I want to be able to print questions from a questions folder in a sequential order,
    enter an answer ,store it and be able to validate a correct answer to a result file in PhaseTest folder

  2. Relevant commands, code, scripts, algorithms:
    Script order
    1.)set up
    2.)username
    3.)question/answer
    4.)calculate the output of the test

  3. The attempts at a solution (include all code and scripts):

#!/bin/sh

echo Username
read i
echo Password
read j
Username=$i":"$j

valid=`grep -n $Username /$HOME/PhaseTest/userinfo | wc -l` 



if [ $valid -eq 1 ]
then
echo "********Welcome to Grounoch Marx University********"
date
#Display questions
cat questions
else
echo "Unauthorised user"
fi 
  1. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):
    De Montfort University,Leicester,Uk,lecturer Parminder,IMAT 5122

Note: Without school/professor/course information, you will be banned if you post here! You must complete the entire template (not just parts of it).

First, let me sincerely thank you for filling out the homework template. You would not believe how many students just can't be bothered...

#!/bin/sh

echo Username
read i
echo Password
read j

So far so good...

Username=$i":"$j

Works, but may break on spaces. Better and less awkward as Username="${i}:${j}"

valid=`grep -n $Username /$HOME/PhaseTest/userinfo | wc -l`
  • You don't need to check what grep prints, you can just check it's return value. It's silent, you don't see it, but all programs have one. Zero means success, nonzero means error.
  • You should put a ^ at the beginning of the pattern, and a $ at the end, to make sure grep has to match the entire and not just part of it.
  • Short form for /${HOME}/ is ~/
# -q means "quiet", preventing grep from printing anything.
# The ! is a boolean 'not', so the if statement only runs if grep fails to find it.
if ! grep -q "^${Username}\$" ~/PhraseTest/userinfo
then
        echo "Invalid user"
        exit 1
fi

echo "********Welcome to Grounoch Marx University********"
...

I need to see what the question file looks like to offer further suggestions.

1 Like

Questions file has questions 1 to 8 as follows:
p { margin-bottom: 0.21cm; } 1. How would you create a new directory called docs?

a) makedocs
b) mkdir docs
c) create docs
d) none of these

  1. How would you delete a file called textFile?

a) delete textFile
b) remove textFile
c) rm textFile
d) none of these

  1. How would you create an empty file called tempFile?

a) cat tempFile
b) empty tempFile
c) create tempFile
d) touch tempFile

  1. What is the command to read the content of a file?

a) cat fileName
b) more fileName
c) less filename
d) all of above

  1. What is the command to display a list of all files, including the hidden files?

a) ls -a
b) ls -all
c) display -hidden
d) ls -l

  1. What is the command to check current working directory?

a) cd
b) ls
c) pwd
e) ped

  1. How would you change permissions of a file called tempFile? The new permissions are rwx for user and for group and others no permission.

a) grant 700 tempFile
b) chmod 700 tempFile
c) grant 711 tempFile
d) chmod 711 tempFile

  1. What is the command to go to parent directory?

a) cd parent
b) cd ..
c) cd .
d) all of above

I need help to print the questions one after the other by making use of "Enter N for next questions and P for previous question" in a command line

Here are some ideas :

1) you should add a specific character before every lines that start by a number.
this way, you can use this character as a record separator and retrieve questions by there records number (easy using awk)

2) you should store the total number of record/questions into a variable, because you may need it :

  • if you are at the last questions and press N "Next" into the menu, you could therefore go to question 1
  • if you are in question 1 and press P "Previous" then you could easily go to the last equestions.

3) Since you are going to do repeted task (display question and ask choice) you should consider building that call into a function that would take (for example) the questions number as arguments

1 Like

thanks it works but noticed this error display on the terminal screen,it is highlighted below:
8. What is the command to go to parent directory?

a) cd parent
b) cd ..
c) cd .
d) all of above

Enter your choice :
b
Warning: unknown mime-type for "Enter question number, N for next question, P for previous question or Q for done" -- using "application/octet-stream"
Error: no such file "Enter question number, N for next question, P for previous quesWarning: unknown mime-type for "Enter question number, N for next question, P for previous question or Q for done" -- using "application/octet-stream"
Error: no such file "Enter question number, N for next question, P for previous question or Q for done"

Question:
1.) where will i place the code to check for my answers.I have an answers file with the correct answers in it. I want to be able to verify my answers
2.) where will place the code to read if a user as attempted the test before to stop such user from taking the test again
3.) how to exit the questions to output the result,say "5 out of 8"

you can add set -x at the beginning of your script to help you to troubleshoot it.
Try to locate the error (in ksh you can insert lines such as (in ksh)

echo "currently in $LINENO" 

If you want people to help you to troubleshoot your code, please post it.

1) locate which portion of code is fooling around.
2) understand what it does
3) read the error message, if sometimes they are not relevant, they often give clues

Reference (to previous post title): Help_Beginner in Unix

I need assistance in terms of troubleshooting this codes.Though it not complete for the required task,shedding of any idea will be appreciated.
the information about the code is the thread referenced above

  #!/bin/ksh
touch fileuseranswer
 
echo Username
read i
echo Password
read j
Username="${i}:${j}"
 
if ( ! grep -q $Username /$HOME/PhaseTest/userinfo )  
then
        echo "Wrong Login/Passwd"
        exit 2
fi
 
echo "********Welcome to Groucho Marx University********"
date
QFILE=./questions
# max questions
max=$(sed 's/^[1-9][0-9]*\. /#&/' ${QFILE} | nawk -v FS="\n" -v RS="#" 'END {print NR}')
 
question(){
sed 's/^[1-9][0-9]*\. /#&/' ${QFILE} | awk -v FS="\n" -v RS="#" -v VAR="$1" '(NR==VAR){print$0}' | tee q.tmp
#sed -n '/^'`echo "$1"`'\./,/^d) /p' ${QFILE}
 
while :
do
echo "Enter your choice :"
read b
if ( ! grep "$b"')' q.tmp >/dev/null ) ; then
        echo "Invalid choice"
        rm q.tmp
        RC=1 ; break
else
        rm q.tmp
        grep -v ^$1 fileuseranswer >fileuseranswer.tmp
        echo "$1 $a" >>fileuseranswer.tmp
        cat fileuseranswer.tmp >fileuseranswer
        rm fileuseranswer.tmp
        RC=0 ; break
fi
done
return $RC
}
 
while :
do
print "Enter question number, N for next question, P for previous question or Q for done"
read a
case $a in
        [1-9]*) [[ $a -gt $max ]] && c=$max || c=$a ; question "$c" ;;
        n|N) [[ $c -lt $max ]] && let c+=1 || let c=1 ; question "$c" ;;
        p|P) [[ $c -ge 1 ]] && let c-=1 || let c=$max ; question "$c" ;;
        q|Q) exit 0 ;;
        *) echo "Wrong choice , try again" ;;
esac
done

thanks

---------- Post updated 12-01-10 at 09:11 AM ---------- Previous update was 11-30-10 at 06:31 PM ----------

1)Please explain or show what the correct line of code should be.
2)notice this error highlighted in the terminal when running the code:
Warning: unknown mime-type for "Enter question number, N for next question, P for previous question or Q for done" -- using "application/octet-stream"
Error: no such file "Enter question number, N for next question, P for previous question or Q for done"

how can I resolve this error.

thanks

do
print "Enter question number, N for next question, P for previous question or Q for done"
read a
...

Why did you use echo in the first test and suddenly "print" instead?

replace print by echo

By the way, read the error message : print expect a file and find something whereas "...." is a string (no such file error)

referring to the code above,how can output my answers and confirm it. the file fileuseranswer does not have the output there, instead it has:
2 n
3 n
4 n
5 n
..etc and I don't know why?
My priority is to be able to confirm the output of the test and displaying the total.
e.g "You got 5 out of 8"

Well we dont know how your code is now... (did you replace print by echo?... what else has been done since?)

I added some line of code for the saving and outputing my result to the filuseranswer file,but was not successful.here is code:

#!/bin/sh
touch fileuseranswer
 echo "**********WELCOME TO GROUCHO MARX UNIVERSITY**********"

echo Username:
read i
echo Password:
read j
Username="${i}:${j}"
 
if ( ! grep -q $Username /$HOME/PhaseTest/userinfo )
then
        echo "Wrong Login/Passwd"
        exit 2
fi
 


echo 
echo -e "                    \c"
date
QFILE=./questions
# max questions
max=$(sed 's/^[1-8][0-8]*\. /#&/' ${QFILE} | nawk -v FS="\n" -v RS="#" 'END {print NR}')
 
question(){
clear
sed 's/^[1-8][0-8]*\. /#&/' ${QFILE} | awk -v FS="\n" -v RS="#" -v VAR="$1" '(NR==VAR){print$0}' | tee q.tmp
#sed -n '/^'`echo "$1"`'\./,/^d) /p' ${QFILE}
 
while :
do
echo "Enter your choice :"
read b
if ( ! grep "$b"')' q.tmp >/dev/null ) ; then
        echo "Invalid choice"
        rm q.tmp
        RC=1 ; break
else
        rm q.tmp
        grep -v ^$1 fileuseranswer >fileuseranswer.tmp
        echo "$1 $a" >>fileuseranswer.tmp
        cat fileuseranswer.tmp >fileuseranswer
        rm fileuseranswer.tmp
        RC=0 ; break
fi
done
return $RC
}
 
while :
do
echo -e "Enter question number, N for next question, P for previous question or Q for done"
read a
case $a in
        [1-8]*) [[ $a -gt $max ]] && c=$max || c=$a ; question "$c" ;;
        n|N) [[ $c -lt $max ]] && let c+=1 || let c=1 ; question "$c" ;;
        p|P) [[ $c -ge 1 ]] && let c-=1 || let c=$max ; question "$c" ;;
        q|Q) exit 0 ;;
        *) echo "Wrong choice , try again" ;;
esac
done

answers(){
 echo -e "\c" > fileuseranswer 
    QNUM=0 
    TOTAL=0 
    while [ $QNUM -lt ${#questions[@]} ] 
    do 
        echo "\"${FILEUSERANSWER[$QNUM]}\" " >> fileuseranswer 
        [ "${CORRECT[$QNUM]}" = "${FILEUSERANSWER[$QNUM]}" ] && let SCORE=SCORE+1 
        let QNUM=QNUM+1 
    done 
    echo "You got $TOTAL questions correct out of ${#questions[@]}" 
}

Please to troubleshoot it.I've gone out of ideas.

Thanks

echo "$1 $a" >>fileuseranswer.tmp

should be replaced by

echo "$1 $b" >>fileuseranswer.tmp

your answers(){ ... } function is useless since not called anywhere.

1 Like

You're Awesome.Thanks its works quiet all right.
1.)I tried adjusting the line of code for the questions because it displays "Enter question number, N for next question, P for previous question or Q for done" at the beginning of the question instead of displaying question 1 and asking for "Enter...." after then.
2.)I need help on how to end the questions at question 8
3.)To be able to go to a previous question done without being able to change the answer

Thanks a lot.Your help is appreciated.

Check this thread

Are you sure your question while loop is working? What happens when you get "invalid choice"?
When you choose Question 2, is it the correct one displayed?
And ctsgnb is right, in order to do what you want, you ought to finish your answer function - and use it...

thanks,saw your reply but I tried adapting the lines of code to my codes but did not work

Please can you highlight the line of code that will work for me in the referred to code.

Thanks

---------- Post updated at 05:56 PM ---------- Previous update was at 03:07 AM ----------

yes, i think it's question while loop is working.
What is displayed after the date is "Enter question number,N for next,P for previous or Q for done", instead of displaying the first question.
When it displays Invalid Choice, it then displays "Enter...." again for the following questions

It displays what: "Enter your choice" again or returns to asking you to choose a new question... ?

I hope i'm not of order by showing you the output from the screen shot

WELCOME TO GROUCHO MARX UNIVERSITY
Username:
john
Password:

                Wed Dec 15 21:31:01 GMT 2010

Enter question number, N for next question, P for previous question or Q for done
n

Enter your answer :
c
Invalid choice
Enter question number, N for next question, P for previous question or Q for done
n

  1. How would you create a new directory called docs?

a) makedocs
b) mkdir docs
c) create docs
d) none of these

Enter your answer :

This is the exact view of the scree shot

So you can see also, there is something not quite right:

Wed Dec 15 21:31:01 GMT 2010
Enter question number, N for next question, P for previous question or Q for done
n

Lets say you are at the beginning of execution and entered next, you have no values to compare with so you should force the program to call Question 1 and display it...
Why isnt it displaying a question since its in now the answer loop?
More strange - in answer loop, it judged the answer as invalid - It just NOT come out of loop but display and accept your choice again (till a valid choice...).