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
# 8  
Old 12-05-2013
Hi it means the if condition is not working correctly

can you try this

Code:
if [[ ( "$date" -eq "$DAT" ) && ( "$day" = "$DAY" ) && ( "$month" = "$MON" ) ]

This User Gave Thanks to zozoo For This Post:
# 9  
Old 12-05-2013
@RudiC dude i need only those names which satisfy the IF condition, but currently the entire name list is getting displayed. thats my problem!

---------- Post updated at 06:07 AM ---------- Previous update was at 05:48 AM ----------

Code:
if [ ("$date" -eq "$DAT") && ("$day" = "$DAY") && ("$month" = "$MON") ]

Message: ./new.sh[15]: syntax error at line 18 : `(' unexpected

Code:
if [ "$date" -eq "$DAT" && "$day" = "$DAY" && "$month" = "$MON" ]

It gives all the names as before

'IF' is not getting executed properly Smilie donno Y!
# 10  
Old 12-05-2013
try this
Code:
if  [[ "$date" -eq "$DAT" && "$day" = "$DAY" &&  "$month" = "$MON" ]] ; then

---------- Post updated at 05:15 PM ---------- Previous update was at 04:53 PM ----------

please find below test snippet

Code:
/var/backup # cat test.sh                                                         
while IFS=',' read i j k                                                        
do                                                                              
if [[ $i -eq "06" && $j = '12' && $k = '2013' ]];                               
then                                                                            
echo $i >> new.txt                                                              
fi                                                                              
done < input                                                                    
/var/backup # cat input                                                           
06,12,2013                                                                      
/var/backup # ./test.sh                                                           
/var/backup # cat new.txt                                                         
06                                                                              
/var/backup #


Last edited by zozoo; 12-05-2013 at 07:43 AM.. Reason: fixed code tags
# 11  
Old 12-05-2013
Looking at your execution log
Code:
+ [ 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 ]

it's surprising that anything is printed because every test is FALSE.
However, if I execute (in bash!)
Code:
while IFS=',' read name date day month year
  do  if [ "$date" -eq "$DAT" -a "$day" = "$DAY" -a "$month" = "$MON" ]
        then echo $name
      fi
  done < file
sreenadh

, you can see that the result is what you want. Are you sure you're sharing your code as is? Try the -v option.
# 12  
Old 12-05-2013
Code:
#!/bin/ksh
#Initial Variables
IFILE="$HOME/bday.csv"
OFILE="bday_out"$$
MAILID="abc@xxx.com"
#get today's date & month
DAT=`(date '+%d')`
DAY=`(date '+%a')`
MON=`(date '+%b')`
while IFS=',' read name date day month year
do
set -x
if [ "$date" -eq "$DAT" -a "$day" = "$DAY" -a "$month" = "$MON" ]
set +x
   then echo $name
   fi
done < $IFILE > $OFILE
set -x
if [ -f $OFILE -a -s $OFILE ]
set +x
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

This is the code i am executing & i am trying this in my office, dont have bash here! Smilie

---------- Post updated at 08:08 AM ---------- Previous update was at 08:04 AM ----------

---------- Post updated at 08:09 AM ---------- Previous update was at 08:08 AM ----------

Quote:
Originally Posted by zozoo
try this
Code:
if  [[ "$date" -eq "$DAT" && "$day" = "$DAY" &&  "$month" = "$MON" ]] ; then

---------- Post updated at 05:15 PM ---------- Previous update was at 04:53 PM ----------

please find below test snippet

Code:
 
/var/backup # cat test.sh                                                         
while IFS=',' read i j k                                                        
do                                                                              
if [[ $i -eq "06" && $j = '12' && $k = '2013' ]];                               
then                                                                            
echo $i >> new.txt                                                              
fi                                                                              
done < input                                                                    
/var/backup # cat input                                                           
06,12,2013                                                                      
/var/backup # ./test.sh                                                           
/var/backup # cat new.txt                                                         
06                                                                              
/var/backup #


Tried this IF condition buddy, but still the result is same Smilie
Is it because i am executing it in Ksh ? i cant do it in bash though!
# 13  
Old 12-05-2013
Strange enough. Well, last resort: Run the entire script with set -vx and attach the entire execution log here.
# 14  
Old 12-05-2013
Please find the log here

Code:
+ [[ 05 -eq 05 ]]
+ [[ Thu = Thu ]]
+ [[ Dec = Dec ]]
+ [[ 05 -eq 05 ]]
+ [[ Thu = Thu ]]
+ [[ December = Dec ]]
+ [[ 06 -eq 05 ]]
if [ -f $OFILE -a -s $OFILE ]
set +vx
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
+ [ -f bday_out1560592 -a -s bday_out1560592 ]
sed: illegal option -- i
Usage:  sed [-n] [-u] Script [File ...]
        sed [-n] [-u] [-e Script] ... [-f Script_file] ... [File ...]
Birthday mail sent

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