Register
It is currently Thu Aug 21, 2014 9:59 am

a script to check for mail:


All times are UTC - 6 hours


Post new topic Reply to topic  [ 12 posts ] 
Author Message
 PostPosted: Tue Apr 25, 2006 6:02 am   

Joined: Tue Apr 25, 2006 5:27 am
Posts: 6
Location: Quebec, Canada
HI
I am a newbie at script writting:

I am in the process of learning scripting using bash:
The book i use is UNIX SHELLS BY EXAMPLE by Ellie Quigley

I have to write a script that will check for new mail and write a
message to my screen; This is how the question is written:

The program will get the size of the mail spool file for the user.
The script will execute in a contunuous loop,once every 30 seconds. Each time the loop executes, it will compare the size of the mail spool file with its size from the previous loop. If the new size is greater than the old size, a message will be printed on my screen, saying (Username, You have mail).

From what i know so far bash as already a mail program.
All i have to do is type mail and it tell me if i got mail.
If i understand this correctly this is not email from the web or freinds:
Its mail from lets say if i was connected to a network, is this correct:
This one got me stump: :o
Maybe it because it to easy i gest: :oops:
I am using suse 10.0
Got your web site from suse cool solutions page:


Well thanks in advance:


Top
 Profile  
 PostPosted: Tue Apr 25, 2006 2:41 pm   
Site Admin
User avatar

Joined: Sun May 15, 2005 9:36 pm
Posts: 666
Location: Des Moines, Iowa
pappy wrote:
HI
I am a newbie at script writting:

I am in the process of learning scripting using bash:
The book i use is UNIX SHELLS BY EXAMPLE by Ellie Quigley

I have to write a script that will check for new mail and write a
message to my screen; This is how the question is written:

The program will get the size of the mail spool file for the user.
The script will execute in a contunuous loop,once every 30 seconds. Each time the loop executes, it will compare the size of the mail spool file with its size from the previous loop. If the new size is greater than the old size, a message will be printed on my screen, saying (Username, You have mail).

From what i know so far bash as already a mail program.
All i have to do is type mail and it tell me if i got mail.
If i understand this correctly this is not email from the web or freinds:
Its mail from lets say if i was connected to a network, is this correct:
This one got me stump: :o
Maybe it because it to easy i gest: :oops:
I am using suse 10.0
Got your web site from suse cool solutions page:


Well thanks in advance:


hmmm well, lets help you get started ;)

program will get the size of the mail spool file for the user.
perhaps something like
Code:
du /var/mail/$USER


once every 30 seconds.
Code:
sleep 30


a message will be printed on my screen, saying (Username, You have mail).
Code:
echo "$USER, You have mail"



See if that helps get you started ;)


Top
 Profile WWW  
 PostPosted: Wed Apr 26, 2006 6:48 am   

Joined: Tue Apr 25, 2006 5:27 am
Posts: 6
Location: Quebec, Canada
crouse wrote:
pappy wrote:
HI
I am a newbie at script writting:

I am in the process of learning scripting using bash:
The book i use is UNIX SHELLS BY EXAMPLE by Ellie Quigley

I have to write a script that will check for new mail and write a
message to my screen; This is how the question is written:

The program will get the size of the mail spool file for the user.
The script will execute in a contunuous loop,once every 30 seconds. Each time the loop executes, it will compare the size of the mail spool file with its size from the previous loop. If the new size is greater than the old size, a message will be printed on my screen, saying (Username, You have mail).

From what i know so far bash as already a mail program.
All i have to do is type mail and it tell me if i got mail.
If i understand this correctly this is not email from the web or freinds:
Its mail from lets say if i was connected to a network, is this correct:
This one got me stump: :o
Maybe it because it to easy i gest: :oops:
I am using suse 10.0
Got your web site from suse cool solutions page:


Well thanks in advance:


hmmm well, lets help you get started ;)

program will get the size of the mail spool file for the user.
perhaps something like
Code:
du /var/mail/$USER


once every 30 seconds.
Code:
sleep 30


a message will be printed on my screen, saying (Username, You have mail).
Code:
echo "$USER, You have mail"



See if that helps get you started ;)


Well this is what i have so far;
the formula give me a loop every 30 second and give me the
right info ($user, You have mail) but;
on the second pass if i send myself a new mail it dosen't evaluate the old wc -l count with the new wc -l count:
As you can see i have to loop every 30 second and if i recived
some new mail it as to tell me (I have new mail):
I tried every way i could figure out but nothing works;
The book that i am using to learn all of this dosen't give me
the answer and it limited on the example; So i have to use site
like this (and the help of guys like you) to see if i am wright with what i do
{ This is the script i came up with so far}

while [ $USER = (name) ]
do
wc -l /var/spool/mail/$USER
sleep 30
old=`wc -l /var/spool/mail/$USER`
new=`wc -l /var/spool/mail/$USER`
if [ "$old" = "$new" ]
then
echo "$USER, You have mail"
elif [ "$old" != "$new" ]
then
echo "$USER, You have new mail"
fi
done :o


Top
 Profile  
 PostPosted: Wed Apr 26, 2006 8:40 am   
Site Admin

Joined: Tue May 17, 2005 7:31 pm
Posts: 251
Location: Georgia
pappy wrote:
Code:
while [ $USER = (name) ]
do
wc -l /var/spool/mail/$USER
sleep 30
old=`wc -l /var/spool/mail/$USER`
new=`wc -l /var/spool/mail/$USER`
if [ "$old" = "$new" ]
then
echo "$USER, You have mail"
elif [ "$old" != "$new" ]
then
echo "$USER, You have new mail"
fi
done

one thing you can do... is check to see where the script is bombing.
like directly after assigning the variables, "old" and "new", see what they were assigned with.
Code:
echo "${old} is old amount"; echo "${new} is the new amount"


I think you will see that old and new have stuff other than numbers :)

YMMV

Now, is it telling you the same message everytime? The "You have mail" or is it saying "You have new mail" everytime?

one thing I would change too... just for testing... is the elif statement.

instead of:
Code:
elif [ "$old" != "$new" ]

just use
Code:
else

and see which message you get everytime.
and post :)


Top
 Profile  
 PostPosted: Thu Apr 27, 2006 6:51 am   

Joined: Tue Apr 25, 2006 5:27 am
Posts: 6
Location: Quebec, Canada
[/code][/quote]
one thing you can do... is check to see where the script is bombing.
like directly after assigning the variables, "old" and "new", see what they were assigned with.
Code:
echo "${old} is old amount"; echo "${new} is the new amount"


I think you will see that old and new have stuff other than numbers :)

YMMV

Now, is it telling you the same message everytime? The "You have mail" or is it saying "You have new mail" everytime?

one thing I would change too... just for testing... is the elif statement.

instead of:
Code:
elif [ "$old" != "$new" ]

just use
Code:
else

and see which message you get everytime.
and post :)[/quote]

Well did what you guys said:
I rewrote the script this way'

while [ $USER = (name) ]
do
old=`wc -l /var/spool/mail/$USER`
new=`wc -l /var/spool/mail/$USER`
if [ "$old" = "$old" ]
then
echo "$old"
echo "$USER, You have mail"
else [ "$new" != "$old" ]
echo "$new"
echo "$USER, You have new mail"
fi
done
It stays at the ("$USER, You have mail")
it never pass that line even if i send myself new mail.
it as if it dosent evalluate the new (wc -l) of the new message.
It echo ($old) all the time:never the ($new). The only thing that
works is the loop;
From what i can see, is it add the new count to the old:
It never pass the first (if) dosent go to the (else) for the new count::cry:
I am not good enough to debug this scripts yet: I just can't figure it out; :(


Top
 Profile  
 PostPosted: Thu Apr 27, 2006 8:16 am   
Site Admin

Joined: Tue May 17, 2005 7:31 pm
Posts: 251
Location: Georgia
ok... let me explain what is going on...
you have a script running a loop...
inside that loop you are assigning the variable old... and the variable new
you are then asking if old is the same as old...
ofcourse it is :D
you just told it to be...
so... try this...
OUTSIDE the loop... right before the while... put the old=`blah`
then... inside the loop... assign new just like you have it...
then... the if statement shouldn't read if [ "$old" = "$old" ]
it should read if [ "$new" = "$old" ]

then... right after the part that says "you have NEW mail"
add the line... old="$new"

and you should have yourself a nice working script :)


Top
 Profile  
 PostPosted: Fri Apr 28, 2006 6:18 am   

Joined: Tue Apr 25, 2006 5:27 am
Posts: 6
Location: Quebec, Canada
jbsnake wrote:
ok... let me explain what is going on...
you have a script running a loop...
inside that loop you are assigning the variable old... and the variable new
you are then asking if old is the same as old...
ofcourse it is :D
you just told it to be...
so... try this...
OUTSIDE the loop... right before the while... put the old=`blah`
then... inside the loop... assign new just like you have it...
then... the if statement shouldn't read if [ "$old" = "$old" ]
it should read if [ "$new" = "$old" ]

then... right after the part that says "you have NEW mail"
add the line... old="$new"

and you should have yourself a nice working script :)


A big thanks for the help I sure need it:
I finally got the script working in the wright direction:
Like i said the book i am using to learn scripting does'nt
give answer so i don't know if i am wright or wrong, or if its working properly:
in the wright direction: :?
Again Thanks


Top
 Profile  
 PostPosted: Fri Apr 28, 2006 10:15 am   
Site Admin

Joined: Tue May 17, 2005 7:31 pm
Posts: 251
Location: Georgia
you're very welcome...
post what you have so we can see how you fixed it...

i'm interested to see which angle you took...
i feel scripting is like an artform... there are different ways to put paint on the canvas... how do you do it?

o ya... and welcome to Bashscripts.org... if you havn't already... check out usalug.org, and if you use suse... opensuse.us :)
they are all offiliated


Top
 Profile  
 PostPosted: Wed May 03, 2006 4:29 am   

Joined: Tue Apr 25, 2006 5:27 am
Posts: 6
Location: Quebec, Canada
jbsnake wrote:
you're very welcome...
post what you have so we can see how you fixed it...

i'm interested to see which angle you took...
i feel scripting is like an artform... there are different ways to put paint on the canvas... how do you do it?

o ya... and welcome to Bashscripts.org... if you havn't already... check out usalug.org, and if you use suse... opensuse.us :)
they are all offiliated


Sorry for the delay was away for a few days.
Here is the script i wrote for the mail loop.

old=""
do
while [ "USER" = (name) ]
new=`/var/spool/mail/USER`
if [ "new" = "old" ]
echo "USER, You have mail"
else [ "new" != "old" ]
echo "USER< You have new mail"
fi
sleep 30
done
It does what it supposed to do acording to the question in
the book i am using.
Well I hope i did it okay this time it.
Once again THANKS. :lol:


Top
 Profile  
 PostPosted: Wed May 03, 2006 7:30 am   
Site Admin

Joined: Tue May 17, 2005 7:31 pm
Posts: 251
Location: Georgia
hey there pappy... gladd you got it sorted out...
Code:
old=""
do
while [ "USER" = (name) ]
new=`/var/spool/mail/USER`
if [ "new" = "old" ]
echo "USER, You have mail"
else [ "new" != "old" ]
echo "USER< You have new mail"
fi
sleep 30
done

i'm thinking i would have done something like...
Code:
old=`cat /var/spool/mail/$USER | wc -l`
while [ $USER = "bob" ]
do
    new=`cat /var/spool/mail/$USER | wc -l`
    if [ "$new" = "$old" ]
    then
        echo "${USER}, no change in mail"
    elif [ "${new}" -gt "$old" ]
        echo "${USER}, you have new mail!"
        old=$new
    else
        echo "${USER}, did you delete mail?"
        old=$new
    fi
    sleep 30
done

the concept of what you wrote makes sense.. but it is NOT syntaxically (yes i just made up that word :) ) correct.
in other words... exactly how you have it above... would error if ran.

in my version, it would run... statically for one user. I would probably make the user more dynamic, and allow the person who runs it to choose what user account to monitor,,, and ofcourse with that comes better error checking and the whole nine... :)


Top
 Profile  
 PostPosted: Wed May 03, 2006 10:24 am   
Site Admin
User avatar

Joined: Sun May 15, 2005 9:36 pm
Posts: 666
Location: Des Moines, Iowa
I just want to hear jbsnake actually pronounce syntaxically :lol: :lol:


Top
 Profile WWW  
 PostPosted: Wed May 10, 2006 6:05 am   

Joined: Tue Apr 25, 2006 5:27 am
Posts: 6
Location: Quebec, Canada
jbsnake wrote:
hey there pappy... gladd you got it sorted out...
Code:
old=""
do
while [ "USER" = (name) ]
new=`/var/spool/mail/USER`
if [ "new" = "old" ]
echo "USER, You have mail"
else [ "new" != "old" ]
echo "USER< You have new mail"
fi
sleep 30
done

i'm thinking i would have done something like...
Code:
old=`cat /var/spool/mail/$USER | wc -l`
while [ $USER = "bob" ]
do
    new=`cat /var/spool/mail/$USER | wc -l`
    if [ "$new" = "$old" ]
    then
        echo "${USER}, no change in mail"
    elif [ "${new}" -gt "$old" ]
        echo "${USER}, you have new mail!"
        old=$new
    else
        echo "${USER}, did you delete mail?"
        old=$new
    fi
    sleep 30
done

the concept of what you wrote makes sense.. but it is NOT syntaxically (yes i just made up that word :) ) correct.
in other words... exactly how you have it above... would error if ran.

in my version, it would run... statically for one user. I would probably make the user more dynamic, and allow the person who runs it to choose what user account to monitor,,, and ofcourse with that comes better error checking and the whole nine... :)


Again sorry for the delay in reply
Okay i will use your advise on the script for like i said i am new
to all of this:
And yes crouse i think i would like to see anyone try to pronouce that word; :lol:


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 12 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 14 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP