#!/bin/bash # states - A state capital guessing game. Requires the state capitals # data file at http://www.intuitive.com/wicked/examples/state.capitals.txt. setglobal 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 } setglobal guesses = '0'; setglobal correct = '0'; setglobal total = '0' while test $guess != "quit" { setglobal 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 setglobal state = $[echo $thiskey | cut -d' ' -f1 | sed 's/-/ /g] setglobal city = $[echo $thiskey | cut -d' ' -f2 | sed 's/-/ /g] setglobal match = $[echo $city | tr '[:upper:]' '[:lower:]] setglobal guess = '"??'" ; setglobal total = $shExpr(' $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! ***" setglobal correct = $shExpr(' $correct + 1 ') setglobal 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