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!
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
Relevant commands, code, scripts, algorithms:
Script order
1.)set up
2.)username
3.)question/answer
4.)calculate the output of the test
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
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).
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) 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
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"
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"
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
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
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...).