#!/bin/bash # states - A state capital guessing game. Requires the state capitals # data file at http://www.intuitive.com/wicked/examples/state.capitals.txt. setvar db = ""/usr/lib/games/state.capitals.txt"" # Format is State[tab]City if test ! -r $db { echo "$0: Can't open $db for reading." >&2 echo "(get http://www.intuitive.com/wicked/examples/state.capitals.txt" >&2 echo "save the file as $db and you're ready to play!)" >&2 exit 1 } setvar guesses = '0'; setvar correct = '0'; setvar total = '0' while test $guess != "quit" { setvar thiskey = "$(randomquote $db)" # $thiskey is the selected line. Now let’s grab state and city info, # then also have "match" as the all-lowercase version of the city name setvar state = "$(echo $thiskey | cut -d\ -f1 | sed 's/-/ /g')" setvar city = "$(echo $thiskey | cut -d\ -f2 | sed 's/-/ /g')" setvar match = "$(echo $city | tr '[:upper:]' '[:lower:]')" setvar guess = ""??"" ; setvar total = $(( $total + 1 )) ; echo "" echo "What city is the capital of $state?" # Main loop where all the action takes place. Script loops until # city is correctly guessed, or the user types "next" to # skip this one, or "quit" to quit the game while [ "$guess" != "$match" -a "$guess" != "next" -a "$guess" != "quit" ] { /bin/echo -n "Answer: " read guess if test $guess = $match -o $guess = $city { echo "" echo "*** Absolutely correct! Well done! ***" setvar correct = $(( $correct + 1 )) setvar guess = "$match" } elif test $guess = "next" -o $guess = "quit" { echo "" echo "$city is the capital of $state." # what you SHOULD have known  } else { echo "I'm afraid that's not correct." } } } echo "You got $correct out of $total presented." exit 0