User avatar
drewibbo
Posts: 41
Joined: Tue Feb 26, 2013 11:54 am
Location: Worthing, UK
Contact: Website

How to include random questions into a bash script.

Sun Apr 21, 2013 8:43 pm

Hi you lovely people,

I have made the following script. It is a quiz for two players that lights LEDs when you get your question correct. It is all working perfectly so far but I want to include random questions from a file or array and use them in the script. Can any one help me? The code is below:

Code: Select all

## Functions...
setup ()
 {
   ## Set the gpio's to outputs...
   echo Setup
   for ia in 0 1 2 3 4 5 6 8 9 10 11 12 13 14 ; do gpio mode $ia out ; done
 }

test ()
 {
   f=1
   while [ $f -lt 10 ]
   do
     echo "Testing the game board... Please wait..."
     ## Flash the LEDs on and off quickly (every millisecond) and repeat 10 times...
     for ib in 0 1 2 3 4 5 6 8 9 10 11 12 13 14 ; do gpio write $ib 1 ; done
     sleep 0.1
     for ic in 0 1 2 3 4 5 6 8 9 10 11 12 13 14 ; do gpio write $ic 0 ; done
     sleep 0.1
     f=$(($f+1))
   done
}

playerone ()
 {
   ## Ask player one to input their name...
   read -p "What is your name Player 1 : " pone
 }

playertwo ()
 {
   ## Ask player two to input their name...
   read -p "What is your name Player 2 : " ptwo
 }

playthegame ()
 {
  ## Set the gpio to start from for each player (Considering breadboard has two lanes)...
  ponescore=0
  ptwoscore=8
  i=1
  while true
  do
    if [[ $i -eq 1 ]]
    then
      ## Enter player one question...
      echo "$pone : What is the capital of England?"
      read answerone
       if [ "$answerone" == "london" ] 
       then
        gpio write $ponescore 1
        echo "Well done $pone! Correct answer."
        i=2
        ponescore=$(($ponescore + 1))
        continue
       else
        echo "Oops! Wrong answer..."
        ponescore="$ponescore"
        i=2
        continue
       fi
    fi
    if [[ $i -eq 2 ]]
    then
      ## Enter player two question...
      echo "$ptwo : What is 10 + 59 = ?"
      read answertwo
       if [ "$answertwo" == "69" ] 
       then
        gpio write $ptwoscore 1
        echo "Well done $ptwo! Correct answer."
        i=1
        ptwoscore=$(($ptwoscore + 1))
        continue
       else
        echo "Oops! Wrong answer..."
        ptwoscore="$ptwoscore"
        i=1
        continue
       fi
    fi
  done
 }

## Reset the scores for players...
pones=0
ptwos=0

while true
do
 setup
 test
 playerone
 playertwo
 playthegame
done
I would like the question to be inserted into the section "## Enter player one question..." and "## Enter player two question..."

Thanks.
Pi Pi Pi...

raspem
Posts: 26
Joined: Mon Apr 15, 2013 7:14 am

Re: How to include random questions into a bash script.

Mon Apr 22, 2013 10:48 am

There are two problems:
- Generate random numbers
- Pick a line from something

There's no way to do this in bash itself, so you have to rely on other commands and using a file (since it doesn't really have arrays). There are various options. Here are two possibilities (but I'm sure people can come up with more):

Use openssl for random number generation:

Code: Select all

openssl rand -hex 2
will return two random bytes in hex code (unfortunately, so you have to convert it to something more useful).

Use ed to pick a line from a file:

Code: Select all

ed -s thefile.txt <<-EOF
N p
EOF
where N is the number.

(Needless to say, it would be a lot easier to do this in some other language than a shell script, ruby for instance. ;) )
My other computer is a MacBook Pro

scrapheap
Posts: 20
Joined: Wed Feb 13, 2013 5:13 pm

Re: How to include random questions into a bash script.

Mon Apr 22, 2013 11:45 am

Picking random questions from an array in bash isn't actually that hard (assuming you have a version of bash that has support for arrays). Try the following code that defines two arrays (one for questions and one for answers) works out the length of the arrays and stores it in the QL variable. It then uses the RANDOM special variable to pick and random number, divides it by the QL variable and puts the remainder in the I variable (the modulo '%' operator makes that nice and easy). Now that you have the I variable you can use that to pull out the randomly chosen question and the related answer.

Code: Select all

#!/bin/bash
Q[0]="Question 1?"
A[0]="Answer 1"
Q[1]="Question 2?"
A[1]="Answer 2"
Q[2]="Question 3?"
A[2]="Answer 3"
Q[3]="Question 4?"
A[3]="Answer 4"
Q[4]="Question 5?"
A[4]="Answer 5"
Q[5]="Question 6?"
A[5]="Answer 6"

QL=${#Q[@]}

let I=$RANDOM%$QL

echo "Chosen question $I"

echo ${Q[$I]}
echo ${A[$I]}

User avatar
drewibbo
Posts: 41
Joined: Tue Feb 26, 2013 11:54 am
Location: Worthing, UK
Contact: Website

Re: How to include random questions into a bash script.

Mon Apr 22, 2013 12:01 pm

Thank you both sooooo musch for your help. Will test it later. :D
Pi Pi Pi...

Return to “Other programming languages”