#!/bin/sh # # notice that different version of ls may return different file # information differently. Some verision of ls return the file size # in the fourth field and thus $5 should be replaced with $4 below. # # save current status of the mail box. # set -- `ls -l $MAIL` previous_size=$5 set -- `wc -l $MAIL` previous_length=$1 while true do # Do a sleep so less computation power is used. sleep 5 # # get new status of mail box # set -- `ls -l $MAIL` current_size=$5 set -- `wc -l $MAIL` current_length=$1 # # if new mails arrived (note that this script doesn't consider # mail deletion. You may want to handle it # as an exercise) # if [ $current_size -gt $previous_size ] then echo New mail received! extra_line=`expr $current_length - $previous_length` echo The mail file has $extra_line more new lines echo Here is the contents: echo tail -$extra $MAIL # # remember current status of mail box # previous_size=$current_size previous_length=$current_length fi done