Accumulate counter in script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Accumulate counter in script
# 1  
Old 05-06-2008
Accumulate counter in script

Hi,

I'm new to unix and have a problem? I'm writing a basic script in ksh and it is a basic quiz with 5 questions. I need to be able to accumulate the correct answers at the end and echo out the total correct answers, I cannot work it out? Please see script so far. If anyone can help that will be great?

#!/bin/ksh

usage()
{
if [ $? -eq " " ]
then
printf "\n "usage": $0 (ANSWER REQUIRED A,B,C) \n\n" >&2
else
STATUS="1"
exit 1
fi

}

STATUS=" "
while [ "$answer" = "" ]
do

printf "\nQUESTION 1\n\n"

printf "Who scored the winning goal for Everton in the FA CUP Final of 1995 against Manchester United?: \n\n"
printf "A) Paul Rideout\n"
printf "B) Duncan Ferguson\n"
printf "C) Matt Jackson\n\n"

printf "the answer is: "

read answer

if [ "$answer" = "" ]
then
usage

fi

done

if [[ $answer = [aA] ]]
then
banner "correct"
else
banner "incorrect"

fi

##################################################

STATUS=" "
while [ "$answer2" = "" ]
do

printf "\nQUESTION 2\n\n"

printf "Who has scored the most goals for Everton this season?: \n\n"
printf "A) Tim Cahill\n"
printf "B) Yakubu\n"
printf "C) Joleon Lescott\n\n"

printf "the answer is: "

read answer2

if [ "$answer2" = "" ]
then
usage

fi

done

if [[ $answer2 = [bB] ]]
then
banner "correct"
else
banner "incorrect"

fi

##################################################

STATUS=" "
while [ "$answer3" = "" ]
do

printf "\nQUESTION 3\n\n"

printf "Who is Everton's current goalkeeper this season?: \n\n"
printf "A) John Ruddy\n"
printf "B) Ian Turner\n"
printf "C) Tim Howard\n\n"

printf "the answer is: "

read answer3

if [ "$answer3" = "" ]
then
usage

fi

done

if [[ $answer3 = [cC] ]]
then
banner "correct"
else
banner "incorrect"

fi

##################################################

STATUS=" "
while [ "$answer4" = "" ]
do

printf "\nQUESTION 4\n\n"

printf "What nationality is Yakubu?: \n\n"
printf "A) French\n"
printf "B) Nigerian\n"
printf "C) Spanish\n\n"

printf "the answer is: "

read answer4

if [ "$answer4" = "" ]
then
usage

fi

done

if [[ $answer4 = [bB] ]]
then
banner "correct"
else
banner "incorrect"

fi

##################################################

STATUS=" "
while [ "$answer5" = "" ]
do

printf "\nQUESTION 5\n\n"

printf "Which current Everton midfielder has recently retired from international football for the Republic of Ireland?: \n\n"
printf "A) Lee Carsley\n"
printf "B) Phil Neville\n"
printf "C) Steven Pienaar\n\n"

printf "the answer is: "

read answer5

if [ "$answer5" = "" ]
then
usage

fi

done

if [[ $answer5 = [aA] ]]
then
banner "correct"
else
banner "incorrect"

fi

##################################################

### To get total count I have tried to do both way's but it does not work???

#count=0
#if [ $answer* = "correct" ]
#then
# count=`expr $count + 1`
#
#fi
#
#printf "The amount of questions you have answered correctly is:$count \n\n"


count=0
while [[ [$answer*] = "correct" ]]
do

count=`expr $count + 1`

done

printf "The amount of questions you have answered correctly is:$count \n\n"
# 2  
Old 05-06-2008
Initialize count at the beginning, and increment it whenever you find a right answer.

Like this:
Code:
if [[ $answer5 = [aA] ]]
then
banner "correct"
count=`expr $count + 1`
else
banner "incorrect"
fi

# 3  
Old 05-06-2008
Computer

Thanks Krishmaths!

A simple solution in the end, Works great, thanks again.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Homework & Coursework Questions

Help with simple script with counter

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 am to create a script that checks a file for specific attributes. Read, write, execute, if the file is empty,... (13 Replies)
Discussion started by: silencep77
13 Replies

2. UNIX for Dummies Questions & Answers

Automatic counter script

Hello, I am having trouble calculating some numbers and I was hoping someone could help me solve this. I have one file with 1 column and what I'm trying to do is add up the lines until a certain value is reach, then jump to where it last finished counting and continue. so for ex: if I... (27 Replies)
Discussion started by: verse123
27 Replies

3. Shell Programming and Scripting

How to use counter to run the script to limit number?

I want to run my shell script to the limit number.Suppose I know in advance that MAX=5 then I want that my script run 5 times only.Something like below$ vi testingMAX=5COMMAND="ssh -l stpuser VHLDVWSAD001 /projects/st/utils/deploy/deployall.sh >/dev/null 2>&1 &" ; sleep 20;count=0while... (2 Replies)
Discussion started by: anuragpgtgerman
2 Replies

4. Shell Programming and Scripting

Incrementing the date depending on the Counter (parameter) passed to the script

Hello Everyone, I have been trying to complete a shell script where, I need to increment the date depending on the file (depending on the date) availability on the remote server. i.e. Basically, I will be passing a counter (like parameter 1 or 2 or 3 or 4). First I will check for the... (1 Reply)
Discussion started by: filter
1 Replies

5. Shell Programming and Scripting

How to display a counter in shell script?

Hi, I am writing a script which processes large number of files in a directory. I wanto display a counter which increment after processing each file. I am processing each file in a for loop. If I echo a variable with its value increasing with each file, I will get around 5000 lines as output.... (10 Replies)
Discussion started by: jaiseaugustine
10 Replies

6. Shell Programming and Scripting

trying to accumulate a number in the same variable

Hello, I trying to accumulate the same number in a variable: #!/usr/bin/sh infile="$1/$2" if ] then #echo "file is not readable: $infile" exit 1 fi awk ' { num_cols=NF; # the number of columns in the current line temp_cols=temp_cols+num_cols #add up... (1 Reply)
Discussion started by: script_op2a
1 Replies

7. Shell Programming and Scripting

Reset the counter in shell script

I am executing the following script using 'awk -f process.awk out' where 'out' is the input file which consists of 5000 sequences. Each time it takes one sequence, run the below program by creating a directory, run the mfold command within that directory, running another shell script 'final5' and... (2 Replies)
Discussion started by: kswapnadevi
2 Replies

8. Shell Programming and Scripting

cant get a counter to work in bash scipt, this is calling expect script

I have looked high and low, tryed lots of diffrent things but cant get a simple counter to work right. what i need is to increase a count ever time it finishes the test, pass or fail. example TEST PASS 1, NEXT TEST PASS 2, I curently have set foo o while {$foo <=5} { incr foo puts... (1 Reply)
Discussion started by: melvin
1 Replies

9. Shell Programming and Scripting

Counter Script..help please

I have generated a script that will email a list of people if a certain PID is not running, using "mailx". I have the script running every 5 minutes as a cron job. I want the script to stop sending an email, if the email has been sent 5 times (meaning PID is dead). I want this so that my... (3 Replies)
Discussion started by: Sunguy222
3 Replies

10. UNIX for Dummies Questions & Answers

Error in Script (counter)

Hello: Executing following script i'm getting error: 1=1+1: 0403-058 Assignment requires an lvalue. It's not assuming the counter but i don't know why. Some hint? Thank you very much in advance. #!/bin/ksh <path>/tiempos.txt num_exe=1 TIEMPOS=<path>/tiempos.txt while ] do (2 Replies)
Discussion started by: Felix2511
2 Replies
Login or Register to Ask a Question