LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Closed Thread
  Search this Thread
Old 12-09-2009, 02:52 PM   #1
allenms
LQ Newbie
 
Registered: Dec 2009
Posts: 7

Rep: Reputation: 0
Randomizing script quit working


Help! One of my scripts quite and I'm at a lose to see why. Need some assistance as I can't see the forest from the trees here. I use the script to randomize questions but something (someone) changed it and it no longer functions prpoerly. Can someone eyeball this and help me out? Thanks,
Al

$ cat quiz
#!/bin/bash
# Script to randomize questions
#=========================
function initialize ()
{
trap 'summarize ; exit 0' INT # Handle user interrupts
num_ques=0 # Number of questions ask so far
num_correct=0 # Number answered corredtly so far
first_time=true # true until first question is ask
cd ${QUIZDIR:=/usr/games/quiz} || exit 2
}

#=========================
function choose_subj ()
{
subjects=($(ls))
PS3="Choose a subject for the quiz from the preceding list: "
select Subject in ${subjects[*]}; do
if [[ -z "$Subject" ]]; then
echo "No subject chosen. Bye." >&2
exit 1
fi
echo $Subject
return 0
done
}

#========================
function exchange ()
{
temp_value=${questions[$1]}
questions[$1]=${questions[$2]}
questions[$2]=$temp_value
}

#========================
function scramble ()
{
typeset -i index quescount
questions=($(ls)
quescount=${#questions[*]} # Number of elements
((index=quescount-1))
while [[ $index > 0 ]]; do
((target=RANDOM % index))
exchange $target $index
((index -= 1))
done
}

#========================
function ask ()
{
exec 3<$1
read -u3 ques || exit 2
read -u3 num_opts || exit 2
index=0
choices=()
while (( index < num_opts )) ; do
read -u3 next_choice || exit 2
choices=("${choices[@]}" "$next_choice")
((index += 1))
done
read -u3 correct_answer || exit 2
exec 3<&-
if [[ $first_time = true ]]: then
first_time=false
echo -e "You may press the interupt key at any time to quit."
fi

PS3=$ques" " #make $ques the prompt for select
# and add some spaces for legibility.
select answer in "${choices[@]}"; do
if [[ -z "$answer" ]]; then
echo "Not a valid choice, please choose again."
elif [[ "$answer" = "$correct_answer" ]]; then
echo "Correct!"
return 1
else
echo "No, the correct answer is $correct_answer."
return 0
fi
done
}
#========================
function summerize ()
{
echo #Skip a line

if
(( num_ques == 0 )); then
echo "You did not answer any questions"
exit 0
fi

(( percent=num_correct*100/num_ques ))
echo "You answered $num_correct questions correctly, out of \ $num_ques total question."

echo " \Your score is $percent percent."
}

#========================
# Main program
initialize # Step 1 in top-level design
subject=$(choose_sugj) # Step 2
[[ $? -eq 0 ]] || exit 2 # If no valid choice, exit

cd $subject || exit 2 # Step 3
echo # Skip a line
scramble # Step 4
for ques in ${questions(*)}; do # Step 5
ask $ques
result=$?
(( num_ques=num_ques+1 ))
if [[ $result == 1 ]]; then
(( num_correct += 1 ))
fi
echo # Skip a line between questions
sleep ${QUEZDELAY:=1}
done

summarize # Step 6
exit 0
 
Old 12-09-2009, 03:21 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Now to me this looks like you've been given the homework to debug this
script. There were a few errors strewn into it quite deliberately as
far as I can tell.

If it's YOUR script you should be able to fix it. If it's your HOMEWORK
I expect the very same thing; YOU fix it.


Cheers,
Tink

P.S.:
I'm closing this thread; if you feel this is unjustified, please contact
me via e-Mail.
 
  


Closed Thread



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Start a script after GDM autologin, don't quit when script finishes Plastech Linux - Newbie 2 05-29-2007 10:15 AM
X quit working after upgrade from 6.8.2 to 6.9.0 kevypoo Slackware 3 08-07-2006 01:11 AM
Konqueror quit working.. NLZX Linux - General 1 03-28-2005 01:30 AM
azureus quit working spotslayer Linux - Software 0 07-31-2004 02:05 PM
Gnome quit working... TheMusicGuy Linux - Software 1 06-06-2004 12:30 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:49 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration