First of all, I would recomand rewriting it in Perl.
If you want to keep it in bash, try to use signals, something like this
http://www.signaltonoise.net/library...ariables2.html
Or search Google for "bash timeout script"
[in a nutshell]
TimerOn()
{ ( o p e n accolade thingy here, obfuscate by GFY board code)
sleep $TIMELIMIT && kill -s 14 $$ &
# Waits XXX seconds, then sends sigalarm to script.
}
[...]
trap Int14Vector 14
#Int14Vector is a function called when sig 14 is traped by script
See
sleep,
kill and
trap for details.
HIH