#!/bin/sh -u # wait until userid log in, then print a message and exit. # Syntax: $0 userid # Purpose: # Wait until the given userid logs in, then print a message and exit. # The script also exits if $USER (the person running the script) # is no longer logged in. (The script exits when $USER logs off.) # This could be run in the background. # Some comments in this script are "teacher" comments that would not # normally appear in your own shell scripts. # -IAN! idallen@idallen.ca # Use standard search path, friendly umask, ASCII collating and sorting. # Set the language and character set to be ASCII/C standard. # PATH=/bin:/usr/bin ; export PATH LC_COLLATE=C ; export LC_COLLATE LANG=C ; export LANG umask 022 # validate the number of inputs and make sure input isn't empty # if [ $# -ne 1 ] ; then echo 1>&2 "$0: Expecting 1 userid arg; found $# ($*)" exit 1 fi # Transfer $1 to a shell variable to make the script more readable. # (Note that you must do this *after* you check $#, not before!) # userid="$1" if [ -z "$userid" ] ; then echo 1>&2 "$0: Expecting 1 userid; found empty string; nothing done" exit 1 fi echo "Now waiting for $userid to log in ..." # Keep waiting for $userid to log in as long as $USER is still logged in. # (If the person running the script logs off, this script exits too.) # Note the use of the shell "!" operator to reverse the return status. # The return status is set only by the *last* command in a pipeline. # while ! who | grep "$userid" ; do echo "DEBUG still waiting for $userid ..." if ! who | grep "$USER" >/dev/null ; then echo 1>&2 "$0: Userid $USER is no longer logged in." exit 1 fi sleep 30 done echo "$0: Userid $userid is now logged in." exit 0