Watch - Shell and AWK Script

Download this source file


#!/bin/ksh
# A shell script to be run in the background, watch will
# check each minute and report on who logs in and who logs out.
#
# Written by Wayne Pollock, Tampa Florida 1996.

# Security:
PATH=/bin:/usr/bin  # Use standard executables only
umask 066           # Temporary files are created with mode 600

# Temporary files:
new=/tmp/new.$$
old=/tmp/old.$$

# Cleanup when done:
trap 'rm -f $new $old 2>/dev/null' 0 1 2 3 15
who >$old          # Initialize the old list.

# The main program:
while :            # Do forever
do
  who >$new
  diff $old $new
  mv $new $old
  sleep 60
done |awk '/>/ {$1 = "Just logged in:   "; print}
           /</ {$1 = "Just logged out:  "; print}
' # End of awk program