Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 673
  • Last Modified:

Check the email by sh shell script

I would like to have a sh shell script to do the following:

I want to check my email for certain time period, say 60 sec. to see I have received any new mail during that period. I also want to have 3 options :

1. An option that allows me to use the prog to check any    user on the same system, to see if this user has    received any new mail during that period.

2. An option that allows me to control the length of the    time period.

3. An option that allows me to decide how to be informed,    by email of message.
0
clo1
Asked:
clo1
1 Solution
 
clo1Author Commented:
Edited text of question
0
 
df020797Commented:
One way is to check the atime for the mailfiles you want to check. This though takes a C program, cant be done in a sh script. The part when you want a biff function for your own mail, its doable... but why invent something already done... there are thousands of mailbiff:S

0
 
clo1Author Commented:
I can pretty sure a sh shell script can do this task. Please help
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
dhughes111797Commented:
use ls -lu to get time of last access on a mailbox
(when last read)
regular ls -l will show time of last modification (last write)
0
 
lockhartCommented:
You can do it easiest by checking the size of the mail file and using that as your trigger. So something like this would be a good first step.

#!/bin/sh
cd /var/mail
user=`/usr/ucb/whoami`             # or wherever your whoami is
                                   # could use an env vbl
# get your command line options
set -- `getopt u:t:m: $*`
if [ $? != 0 ]; then
    echo $USAGE
    exit 2
fi
for ii in $*; do
    case $ii in
    -u) user=$2; shift;;
    -t) time=$2; shift;;
    -m) method=$2; shift;;
    --) shift; break;;
    esac
done

last_size=0                      # so you'll know if it has mail
                                 # to start
while true
do
   current_size=`ls -l | grep $user | nawk '{print $5}'`
   if [ $last_size -lt $current_size ]; then
       
       echo $user " has mail"    # or trigger something based on
                                 # $method
   else
       echo no mail yet
   fi
   last_size=$current_size
   sleep $time
done

This assumes that the fifth field (space separated) from an ls -l command is the file size. This does most of what you wnat. You can put the stuff in to notify yourself however you want. On solaris there's a nifty program called play that plays sounds that can be fun (especially if you rsh it on someone else's machine and have it play a bunch of stuff ;)

Mike
0
 
lockhartCommented:
PS - I forgot to init the time and method variables. They should be done before the getopts segment so that you can over-ride the default values like with the user variable. I hate the way their font screws up the comments, they were all in the 32nd column.

Mike
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now