Print questions from a questions folder in a sequential order

Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
# 1  
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):

Code:
#!/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

4. 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).

Last edited by DukeNuke2; 11-26-2010 at 03:19 PM..
# 2  
First, let me sincerely thank you for filling out the homework template. You would not believe how many students just can't be bothered...
Code:
#!/bin/sh

echo Username
read i
echo Password
read j

So far so good...
Code:
Username=$i":"$j

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

Code:
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 ~/
Code:
# -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.

Last edited by Corona688; 11-26-2010 at 02:11 PM..
This User Gave Thanks to Corona688 For This Post:
# 3  
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



2. How would you delete a file called textFile?


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


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


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


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


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


5. 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


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


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


7. 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


8. 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
This User Gave Thanks to moraks007 For This Post:
# 4  
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

Last edited by ctsgnb; 11-29-2010 at 06:55 AM.. Reason: Initially posted a too complete solution that wouldn't help you to learn by practice
This User Gave Thanks to ctsgnb For This Post:
# 5  
It worked but error came up

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"
# 6  
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)

Code:
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

Last edited by ctsgnb; 11-30-2010 at 05:28 AM..
# 7  
Help to troubleshoot and correct

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
Code:
  #!/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

Last edited by vbe; 12-01-2010 at 05:22 AM.. Reason: code tags...
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to print missing and keep sequential ordering if not found

The below awk in bold will look for the ids in file1 in $2 of file2 and if they match print the line in file2. If an id is missing or not found in file2 (like BMPR2 in line4 of file1) I can not figure out how to add it to them to the lines in the output as missing in $3 following the same format.... (4 Replies)
Discussion started by: cmccabe
4 Replies

2. UNIX for Dummies Questions & Answers

Help with a few questions?

I am doing exam revision and I have this question can anybody help me answer it? Write the commands required to export a directory called /home/open from one machine and to mount it onto a directory called /home/something on another machine? I have done the usual google searchers but still not... (3 Replies)
Discussion started by: thekinganelka
3 Replies

3. Programming

C++ questions

1.) Let's say I have a list: apple1, apple2, apple3, apple4, apple5 Does anyone know how to display the txt file using c++? Does anyone know how to insert apple6 into the text file using link list and show the whole list including apple6? (1 Reply)
Discussion started by: bscot008
1 Replies

4. Shell Programming and Scripting

Few questions

RANDOM is from 1 - 32767 how can i set it to be from 1 - 6 how can i give every user 100 points at start lets say a game. within the script if i create users will it by default prevent creating users with same name twice. I am a beginner in this field and want to do a little game and... (2 Replies)
Discussion started by: gina
2 Replies

5. Programming

Reading special characters while converting sequential file to line sequential

We have to convert a sequential file to a 80 char line sequential file (HP UX platform).The sequential file contains special characters. which after conversion of the file to line sequential are getting coverted into "new line" or "tab" and file is getting distorted. Is there any way to read these... (2 Replies)
Discussion started by: Rajeshsu
2 Replies

6. AIX

Basic AIX Print Queue Questions

Morning, All I've been doing some work on AIX print queues, troubleshooting some underlying issues we've been having. In the process of this I've uncovered some potential gaps in our knowledge. Our typical setup is remote impact printers (In this case OKIML5520s, with NICs) and local queues... (0 Replies)
Discussion started by: alexop
0 Replies

7. UNIX for Dummies Questions & Answers

few questions

hi , i have got few questions 1) i created a file "-myfile" .how can i remove this?what command shud i issue 2) i used echo "TODAYS DATE" ; DATE..........but date is comoing in second line..how can i make it come in single line? 3) how can i list all files in a directory using for... (5 Replies)
Discussion started by: vivekshankar
5 Replies

8. UNIX for Dummies Questions & Answers

questions

Dear Guys , I have some Questions , i need to have answeres for . 1. now if i took a back up for /etc/passwd and /etc/shadow , like simple copy .. and Install a new system , and paste those two files to the new system ... are the passwords and user info. going to work ?? is it posibble ? ... (1 Reply)
Discussion started by: tamemi
1 Replies

9. Programming

Three Questions

I just want to know Three questions: 1.what is the name of c++ compiler used in Linux/Unix (like gcc compiler used for c ).And please tell me how can i compile the program of c++. 2. I have Devloped a program of Database in C Under Linux/Unix Enviornment and now i want to add some graphics... (4 Replies)
Discussion started by: at_renai2001
4 Replies

Featured Tech Videos