Bash shell scripts
nm

#--------------------------------------------------------------------------
# Script:	nm
# Author: 	Duncan Potter, EUCS, May 1997
# Bash shell script to generate warning of newmail arrival, checking every
# 8 seconds. Suitable for use with Ream and Pine mailers.
#
# Alternative is to use the system variable "MAILCHECK": export MAILCHECK=8.
#
# ------- Requirements:
# $HOME/.procmailrc must exist and contain the following line:
#                      export LOGFILE=$MAILDIR/procmail_log
#
# ------- Principle of script:
# New message information is appended to $HOME/Mail/procmail_log by mail
# system, so operate on this file to obtain required information.
#
# -------- Suggested Implementation:
# After login, check the 'nm' process isn't running : ps -fu username
# Then
# 1) Run script in background mode once login has completed (nm&)
# 2) DO NOT call this script from within .bashrc (spawns endless subprocesses !)
# 3) Alias "logout" command to also shutdown the 'nm' process by placing the 
#    following lines in .bashrc:
#			alias lo="kill %nm;exit"
#			alias logout="kill %nm;exit"
#
#--------------------------------------------------------------------------

while true
# start of script loop
do
   ls -l $HOME/Mail/procmail_log > $HOME/bin/.mailsize
   # Report size of procmail_log file, output to file.
   # This file only changes when new mail arrives, so is independent of
   # actions on the new mail folder which may change that file's size.

   diff $HOME/bin/.mailsize $HOME/bin/.oldmailsize > $HOME/bin/.logfile
   # Compare differences between old and new sizes of mail files. 
   # If same, nothing written, only different if new mail has arrived.
 
   if
	[ -s $HOME/bin/.logfile ] 
        # Perform logical test on size of "logfile", TRUE if non-zero size (newmail).

   then
	SENDER=$(tail -3 $HOME/Mail/procmail_log | grep -i From | cut -d " " -f2,3)
	SUBJ=$(tail -3 $HOME/Mail/procmail_log | grep -i subject)
	echo ' ';echo 'New Mail from: '$SENDER
	echo '     '$SUBJ
   fi
   # echo ;		[; separates UNIX command lines]
   # tail - 3 		[strip last 3 lines from file]
   # cut -d " " -f 2,3 	[extract text from fields 2-3, using the space
   # 			character as a delimiter]
   # Create new string variables based on these actions.

   cp $HOME/bin/.mailsize $HOME/bin/.oldmailsize
   # Update current size of mail file. If mail is unread, then by using the
   # "cp" command the old and new mail size files are made identical. The
   # next check on mail size will not then report a difference and the "new
   # mail" reponse will not be generated.
   
   sleep 8
   # Don't check mail again for specified time   

done
# Correctly end the "while" loop.
# Properly end script
exit