Watching if a user logs in or logs out

The following script watches if a certain user logs in or out of the system. An example use:

watchuser hfinn 10

which will watch if the user hfinn logs in or out every 10 seconds.

#!/bin/sh
# watchuser.sh

case $# in
0) echo 'Usage: ' $prog '<username> <check interval(secs)>'; exit 1;;
esac

name=$1
if test "$2" = ""
then
    interval=60
else
    interval=$2
fi

who | awk '{print $1}' | grep $name >& /dev/null
if test "$?" = "0"
then
    loggedin=true
    echo $name is logged in
else
    loggedin=false
    echo $name not logged in
fi

while true
do
    who | awk '{print $1}' | grep $name >& /dev/null
    if test "$?" = "0"
    then
        if test "$loggedin" = "false"
        then
            loggedin=true
            echo $name is logged in
        fi
    else
        if test "$loggedin" = "true"
        then
            loggedin=false
            echo $name not logged in
        fi
    fi
    sleep $interval
done

Here is another version, written using functions for improved modularity:

#!/bin/bash
# watchuser-with-fns.sh

check_usage() {
    case $# in
    0) echo 'Usage: ' $prog '<username> <check interval(secs)>'; exit 1;;
    esac
}
check_user() {
    who | awk '{print $1}' | grep $name >& /dev/null
    if test "$?" = "0"
    then
        if test "$loggedin" = "false"
        then
            loggedin=true
            echo $name is logged in
        fi
    else
        if test "$loggedin" = "true"
        then
            loggedin=false
            echo $name not logged in
        fi
    fi
}

check_usage $*
name=$1
if test "$2" = ""
then
    interval=60
else
    interval=$2
fi
loggedin=false
check_user $name

while true
do
    check_user $name 
    sleep $interval
done