Using username "simmsric". simmsric@opus.cabrillo.edu's password: Last login: Sun May 27 12:52:35 2007 from dsl-63-249-86-11.cruzio.com _ ('v') //-=-\\ (\_=_/) ~~ ~~ Welcome to Opus Serving Cabrillo College /home/cis130/simmsric $ ls backup class frodo-sim.txt lab2 lab4 lab6 may03 midterm project bin codename lab1 lab3 lab5 lab7 mbox mysetup yoursetup /home/cis130/simmsric $ cd lab7 /home/cis130/simmsric/lab7 $ ls game game.v1 game.v2 game.v3 game.v4 timer timer.v4 /home/cis130/simmsric/lab7 $ cat game #!/bin/bash # # CIS 130 Lab 7 - Guess a number game # # Spring 2007 # # usage game -m # # m is upperlimit of guessing range # Sig 15 is terminate (use kill -15 pid to test this) sig15_handler() { :wq echo "Gotta go, the number is $number" exit 2 } # Sig 10 (sigusr1) is sent by timer script sig10_handler() { echo " " echo "Times up, the number was $number" exit 1 } # Sig 0 (EXIT) cleanup sig0_handler () { # kill $CHILD rm -f game.answer 2> /dev/null } # Print usage usage() { echo "usage: $(basename $0) [ -m ]" >&2 echo " where m = the maximum" exit 1 } # Ignore SIGINT (Cntl-C) and SIGQUIT trap '' SIGINT SIGQUIT # Trap sig 10 & 15 and use functions above to handle these incoming signals trap sig15_handler 15 trap sig10_handler 10 # Trap Signal 0 (EXIT) and do cleanup trap sig0_handler EXIT LIMIT=32000 if [ $# -gt 0 ] then if [ "$1" = "-m" ] then if [ "$2" ] then LIMIT="$2" else usage fi else usage fi fi number=$(($RANDOM % $LIMIT + 1)) #((number++)) done="no" # Put answer in a file for those in the know echo $number > game.answer count=0 # Start a timer and remember timer child PID ./timer $$ 30 & #send PID and number of seconds to play CHILD=$! echo "You have 30 seconds to play this game!" while [ $done = "no" ] do ((count++)) echo -n "Please guess a number between 1 and $LIMIT: " read answer if [ "$answer" -gt "$number" ]; then echo "You are high" elif [ "$answer" -lt "$number" ]; then echo "You are low" elif [ "$answer" -eq "$number" ]; then echo "Congratulations! You are right after $count tries" done="yes" fi done # Kill timer if still running kill $CHILD #rm -f game.answer 2> /dev/null exit 0 /home/cis130/simmsric/lab7 $ cat timer # Timer script # # Gets PID and #secs from another program and then sends signal back # to pid when time is up. # # Usage timer pid secs # if [ $# -gt 0 ] then PID=$1 shift else echo "usage: $(basename $0) PID SECONDS" >&2 exit 1 fi if [ $# -gt 0 ] then SECONDS=$1 else SECONDS=10 fi sleep $SECONDS kill -SIGUSR1 $PID exit /home/cis130/simmsric/lab7 $