How to find whether there is new email or not


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to find whether there is new email or not
# 1  
Old 11-15-2011
How to find whether there is new email or not

Hello All,

I am trying to write a script which will check and informed user that there is a new email. Is any one tell me, how can i find that?

Thanking You,
# 2  
Old 11-16-2011
I am curious why you would feel the need to write such a script, doesn't most shells do that automagically if the MAIL environmental variable is set - see the bash and ksh manual pages. But should you feel the need:
Code:
#! /bin/bash

NAME=$(basename ${0})

if [[ -z "${MAIL:=1}" ]]; then
    echo ${NAME}: missing mailfile 1>&2
    exit 1
fi

FILE=$(mktemp -p ${TMPDIR:-/tmp} ${NAME}.XXXXXXXX)

if [[ ! -f "${FILE}" ]]; then
    echo ${NAME}: unable to create checkfile 1>&2
    exit 1
fi

trap "rm -f ${FILE}" EXIT

while sleep 30; do
    if [[ ! -f "${MAIL}" ]]; then
        continue
    fi

    if [[ ${MAIL} -nt ${FILE} ]]; then
        echo You have mail
        touch -r ${MAIL} ${FILE}
    fi
done

# 3  
Old 11-16-2011
I am doing to learn. Please check my code is as follow

Code:
while true
do
mail -E
stat=`echo $status`
echo $stat
if [ "$stat" -eq 0 ]
then
echo There is new e-Mail to read.
exit 1
else
echo No new e-Mail.
sleep 30
continue
fi
done

It's everytime saying that you have new mail. Infact there is no email.
# 4  
Old 11-18-2011
You want to learn? An excellent reason!

Here are some pointers / suggestions:
  • mail-E
Use the -e option, for as per the mail manual page on my (linux) system:
-e Just check if mail is present in the system mailbox. If yes, return an exit status of zero, else, a non-zero value.
-E If an outgoing message does not contain any text in its first or only message part, do not send it but discard it silently, effectively setting the skipemptybody variable at program startup. This is useful for sending messages from scripts started by cron(8).
mail -E always returns true because you are not sending anything.
  • stat=`echo $status`
I am making an assumption, but it appears you are writing a Bourne-shell derived shell script (ksh, bash, etc), not a csh script. At any rate, the built-in shell variable $? contains the exit status of the previous command. So this statement should have been:
Code:
stat=`echo $?`

When assigning a value of a variable to another variable, there is no need to echo that value, you can simply assign it:
Code:
nvar="$avar"

I highly recommend the use of quotes, it prevents unexpected behavior when a variable contains meta-characters such as "*" or "?". For example:
Code:
a='*'
b=$a

will assign the variable b to be the names of all the files in the current working directory.
In your case, use:
Code:
stat="$?"

(Quotes even though $? is always be numeric? Can't be too careful!)
So now your script begins with:
Code:
while true
do
mail -e
stat=`echo "$?"`
echo "$stat"
if [ "$stat" -eq 0 ]
then

Once you eliminate the echo statement, you eliminate the need for a temporary variable for mail's exit status, so you can simplify to:
Code:
while true
do
mail -e
if [ "$?" -eq 0 ]
then

test (which "[" is an alias for) returns with an exit status of 0 if the expression evaluated to true. And since your are checking if the exit status of mail was 0, you really don't need the expression to check if it is zero, you can simply:
Code:
while true
do
if mail -e
then

Hope this helps!

M.D.L.
# 5  
Old 11-18-2011
Thanks for your wonderful explanation. I really appreciated your help. Thanks once again.

Best Regards,
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to find a file with a specific pattern for current sysdate & upon find email the details?

I need assistance with following requirement, I am new to Unix. I want to do the following task but stuck with file creation date(sysdate) Following is the requirement I need to create a script that will read the abc/xyz/klm folder and look for *.err files for that day’s date and then send an... (4 Replies)
Discussion started by: PreetArul
4 Replies

2. Solaris

Find and sed for an email address in Solaris 10

in Solaris 10 I am able to run: find . -type f -name "copy*" exec grep example.com {} \; and I get results. but when I try to find and sed: find . -type f -name "copy*" exec sed -e 's/user@example\.com/user2@example\.com' {} \; the command executes correctly but doesn't change... (6 Replies)
Discussion started by: os2mac
6 Replies

3. Shell Programming and Scripting

Script to find string & email

Hi I have a query that some of you may be able to help me with if poss? I'd appreciate it very much. I've got a few log files, that I would like to search for a string. When the string is found, i'd then like to email out the line/sting. If not found, i'd like to email out 'no found'... (2 Replies)
Discussion started by: horhif
2 Replies

4. Shell Programming and Scripting

find + tar + gzip + uunecode/email --> in one command?

How to search for all files with matching strings --> find + tar + gzip + uunecode/email them in one command? I am sure there is a right way to pass list of files to tar, then compress tar file. Then send that as attachment using uuencode in one command.. Can we do that!? (3 Replies)
Discussion started by: kchinnam
3 Replies

5. Shell Programming and Scripting

Script to find and email selected files

I am trying to come up with a script that will search for selected files and then email them to me. For example, say I have a directory that has the following files: AA_doug.txt AA_andy.txt BB_john.txt APPLE_mike.txt GLOBE_ed.txt GLOBE_tony.txt TOTAL_carl.txt what is the best way to... (2 Replies)
Discussion started by: coach5779
2 Replies

6. Shell Programming and Scripting

Need to find occurrences of email domains in all files in a directory

Hello Everyone! I trust you are off to a great week! Trying to output the name and count of each uniquely occurring domain in the current directory for a portion of a script I'm building. Here's what I'm stuck on: - Need to find UNIQUE occurences of domains (*@domain.com) in ALL files in... (4 Replies)
Discussion started by: linuxhombre
4 Replies

7. Shell Programming and Scripting

Script to find, grep and email

running a suse linux 10. I'm trying to write a script that searches for a file with certain name and that has been modified less than 30 minutes ago, then search for a certain string in that file and email this string out. I have tried different combinations of find, grep and email but no luck... (7 Replies)
Discussion started by: basisvasis
7 Replies

8. Shell Programming and Scripting

Sending find command results to email

This is probably simple so forgive me... I just want to find all files in a folder created within the last 10 minutes... This is easy: # find /home/folder -cmin -10 If the find command locates any files created in the last ten minutes I want it to send an email alert. I just want to... (3 Replies)
Discussion started by: gardellap
3 Replies

9. UNIX for Dummies Questions & Answers

awk to find the status and send an email

Hi, I have a datafile which has the following data and it can have much more records. The data set is as follows: ISA~00~ ~00~ ~ZZ~F159B ~ZZ~U1CAD ~051215~184 3~U~00200~000011432~0~P~< GS~FA~TC11A~U1CAD~051215~1843~000011432~X~002002 ST~997~0001... (6 Replies)
Discussion started by: isingh786
6 Replies
Login or Register to Ask a Question