periodically checks for the removal of the specified file

Add a -n option to waitfor that inverts the monitoring function. So
waitfor -n sandy
checks for sandy logging off the system, and
waitfor -n -f /tmp/dataout &

#
# Wait until a specified user logs on -- version 3
#

# Set up default values

mailopt=FALSE
interval=60
waitfile=""
negate=FALSE

# process command options

while getopts "f:nmt:" option
do
case "$option"
in
f) waitfile=$OPTARG ;;
n) negate=TRUE ;;
m) mailopt=TRUE ;;
t) interval=$OPTARG ;;
\?) echo "Usage: waitfor [-f FILE] [-n] [-m] [-t n] user"
echo " -f FILE wait for the FILE to be created"
echo " -m be notified by mail"
echo " -n negate the desired test"
echo " -t N check every n secs (default $interval)"
exit 1;;
esac
done

# Make sure a user name was specified

if [ "$OPTIND" -gt "$#" -a -z "$waitfile" ] ; then
echo "Missing user name!" ; exit 2
fi

shiftcount=$(( $OPTIND - 1 ))
shift $shiftcount
user=$1

# Check for user logging on or for the file to appear

if [ -n $waitfile ] ; then
if [ $negate = TRUE ] ; then
while test -e $waitfile ; do
sleep $interval
done
else
until test -e $waitfile ; do
sleep $interval
done
fi
else
if [ $negate = TRUE ] ; then
while who | grep "^$user " > /dev/null ; do
sleep $interval
done
else
until who | grep "^$user " > /dev/null ; do
sleep $interval
done
fi
fi

# When we reach this point, the user has logged on

if [ $negate = TRUE -a -n $waitfile ] ; then
condition="no longer exists"
elif [ $negate = FALSE -a -n $waitfile ] ; then
condition="now exists"
fi

device="$( who | grep "^$user " | cut -c10-16 )"

if [ "$mailopt" = FALSE ] ; then
if [ -n "$waitfile" ] ; then
echo "File $waitfile $condition"
else
if [ $negate = TRUE ] ; then
echo "$user has logged out."
else
echo "$user has logged on to $device"
fi
fi
else
recipient=$(who am i | cut -cl-8)
if [ -n "$waitfile" ] ; then
echo "File $waitfile $condition" | mail $recipient
else
if [ $negate = TRUE ] ; then
echo "$user has logged out." | mail $recipient
else
echo "$user has logged on to $device" | mail $recipient
fi
fi
fi

Computer Science & Information Technology

You might also like to view...

You should NEVER criticize your past or present employer online

Indicate whether the statement is true or false

Computer Science & Information Technology

________ replaced SkyDrive

Fill in the blank(s) with correct word

Computer Science & Information Technology