Sending an Automated Mail with a Condition


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sending an Automated Mail with a Condition
# 1  
Old 12-03-2013
Sending an Automated Mail with a Condition

Code:
#!/bin/ksh
#Initial Variables
IFILE ="$HOME/bday.csv"
OFILE ="bday_out"$$
MAILID = xxx@abc.com
 
#get today's date & month
DAT=`(date '+%d')` 
DAY=`(date '+%a')` 
MON=`(date '+%b')`

while IFS=' ' read name date day month year
do
  
   date=`$date`
   day=`$day`
   month=`$month`
   if [ $date -eq $DAT -a $day = $DAY -a $month = $MON ]
   then
 echo $name
   fi
done < $IFILE > $OFILE
if [ -f $OFILE -a -s $OFILE ]
then
   sed -i '1i The following users celebrate their birthday:\n' $OFILE
   mailx -s "Birthday on: $DAT" $MAILID  < $OFILE
   \rm $OFILE
   echo "Birthday mail sent"
else
   echo "No birthdays today"
fi

CSV File format is
------------------
Code:
XYZ 03 Tue December 2013
ACB 04 Wed December 2013

I am new to unix, i google a lot & tried to modify this code but failed. The Code is not getting executed properly! Can anyone please help me out here.Smilie

Last edited by Scrutinizer; 12-03-2013 at 03:16 AM.. Reason: code tags
# 2  
Old 12-03-2013
For starters with variable assignments, there should be no spaces around the =
Code:
IFILE=$HOME/bday.csv
OFILE=bday_out$$
MAILID=xxx@abc.com

This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 12-03-2013
Code:
while IFS=' ' read name date day month year
do
   date=`$date`
   day=`$day`
   month=`$month`
   if [ $date -eq $DAT -a $day = $DAY -a $month = $MON ]
   then
 echo $name
   fi
done < $IFILE > $OFILE

You can remove the part which is marked in RED
This User Gave Thanks to pravin27 For This Post:
# 4  
Old 12-05-2013
Thanks for the response guys
- scrutinizer - i made that correction after posting this here itself Smilie
- pravin27 i removed those lines but i am not able to execute the program it says

New Code:
Code:
#!/bin/ksh
#Initial Variables
IFILE="$HOME/bday.csv"
OFILE="bday_out"$$
MAILID="sss@ttt.com"
#get today's date & month
DAT=`(date '+%d')`
DAY=`(date '+%a')`
MON=`(date '+%b')`
while IFS=',' read name date day month year
do
   if [ $date -eq $DAT -a day = $DAY -a month = $MON ]
   then
        echo $name
   fi
done < $IFILE > $OFILE
if [ -f $OFILE -a -s $OFILE ]
then
   sed -i '1i The following users celebrate their birthday:\n' $OFILE
   mailx -s "Birthday on: $DAT" $MAILID  < $OFILE
   \rm $OFILE
   echo "Birthday mail sent"
else
   echo "No birthdays today"
fi

CSV File
Code:
sreenadh,05,Thu,Dec,2013

But i am getting the message No Birthday today! but the date given is today's date.

Any suggestions?

Last edited by Scott; 12-05-2013 at 04:07 AM.. Reason: Use code tags, please...
# 5  
Old 12-05-2013
Execute with shell options v and/or x set, then tell us the results of the lines
Code:
if [ $date -eq $DAT -a $day = $DAY -a $month = $MON ]

and
Code:
if [ -f $OFILE -a -s $OFILE ]

. Don't \rm $OFILE but see if it exists after the run.
This User Gave Thanks to RudiC For This Post:
# 6  
Old 12-05-2013
Hi RudiC

Added Set -x debugging command

Please find the result below

+ [ 05 -eq 05 -a day = Thu -a month = Dec ]
+ [ 05 -eq 05 -a day = Thu -a month = Dec ]
+ [ 06 -eq 05 -a day = Thu -a month = Dec ]

+ [ -f bday_out2748666 -a -s bday_out2748666 ]
sed: illegal option -- i
Usage: sed [-n] [-u] Script [File ...]
sed [-n] [-u] [-e Script] ... [-f Script_file] ... [File ...]
Birthday mail sent

This is the content of the file bday_out2748666

sreenadh
sree
abcd

I modified the CSV a bit
------------------------
sreenadh 05 Thu Dec 2013
sree 05 Thu December 2013
abcd 06 Fri Dec 2013

Ideally i should get sreenadh as the name,

output is a mail with

subject Birthday on: 05
contents:
sreenadh
sree
abcd
# 7  
Old 12-05-2013
Please use code tags as required by forum rules!

So - what are your conclusions from those messages?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Red Hat

Problems sending mail: Difference between Mail and Mailx?

Whats the difference between mail and mailx? I'm trying to troubleshoot a problem where I can send mail from server A with this `echo $MESSAGE | mail -s "$SUBJECT" -r $FROM $RECIPIENTS` command but executing the same command from server B throws me this error (Both servers are RHEL) ... (1 Reply)
Discussion started by: RedSpyder
1 Replies

2. Shell Programming and Scripting

if condition in sending mail

hello Gurus i have tp tackle a condition like below I have two files to check for any data in it apart from header if one of the file has data I have to send mail given the file name So far my code is if || ; then (printf "%s\n%s\n" "Please check the below bad files: "... (2 Replies)
Discussion started by: Pratik4891
2 Replies

3. Shell Programming and Scripting

Stop sending mail after certain number of mail

Hi guys... I am busy writing a script to notify me via an mail if my application is down. I have done that. Now I want this script to stop sending mails after five mails were sent but the script should keep on checking the application. When the application is up again that count should be... (5 Replies)
Discussion started by: Phuti
5 Replies

4. UNIX for Dummies Questions & Answers

sending mail

i want to send an email from the unix machine to the windows machine. now windows dont have any specified folder for the mail. mail has to be sent to the email-id like abc@xyz.com unix machine itself can not directly send mail. it has to be transferred via mail server. (11 Replies)
Discussion started by: parmeet
11 Replies

5. Filesystems, Disks and Memory

Sending mail

Hi, I am want to send mails from my aix server using smtp adaptors.How to configure this? i tried with send mail command but it is failing,but what i try with my localhost(my desktop which is not using the aix server) machine i can send mails using the smtp adaptor(simply type telnet... (0 Replies)
Discussion started by: gnanadurai_it
0 Replies

6. HP-UX

hp ux not sending mail

Hi.. In Hp ux box,, mails are not being sent. they are getting queued up in mail queue. (3 Replies)
Discussion started by: kkhan
3 Replies

7. HP-UX

Sending mail thru HP-UX

I got stuckup with the issue of sending mail with attachment from HP-UX. Can any one please help me in resolving the issue. I am giving the criteria which i want below, 1. To address. 2. Cc address. 3. Subject 4. File attachment(the file i am using is :-- filename_`date +%Y%m%d`.CSV)... (1 Reply)
Discussion started by: medisetti
1 Replies

8. UNIX for Dummies Questions & Answers

Sending Mail

Please help me out i want to know how to send email from unix machine to any email-id. mail to be sent is web based mail. (1 Reply)
Discussion started by: parmeet
1 Replies

9. UNIX for Advanced & Expert Users

sending mail

How do I send an email with a subject and an attachment from a command prompt? (3 Replies)
Discussion started by: mskarica
3 Replies

10. UNIX for Dummies Questions & Answers

sending a mail to a mail client

Hi everyone! I'm trying to create a database monitoring script that reads an alert file and sends an error message if it can 'grep' a particular string. Is there a way to send this message to a mail client using SMTP? Even better, is there any place on this site that has these kinds of... (5 Replies)
Discussion started by: solaris73
5 Replies
Login or Register to Ask a Question