#!/bin/bash

if [ $# -eq 1 ]; then
    max=$1
else
    max=100
fi

ans=$[$RANDOM % $max + 1]
num=0
cnt=0
ok=0

echo "*** Guess a number from 1 to $max ***"
echo ""

while [ $ok -eq 0 ]; do
    echo -n "What number do you think (-1 to quit) ? "
    read num
    cnt=$[$cnt + 1]

    if [ $num -lt 0 ]; then
        echo ""
        echo "Too BAD. The answer is $ans."
        exit
    fi

    if [ $num -gt $ans ]; then
        echo "The number is too large."
        echo ""
    elif [ $num -lt $ans ]; then
        echo "The number is too small."
        echo "" 
   else
        echo ""
        echo "You're done. You guessed $cnt time(s)."
        ok=1
    fi
done
