Sending an Automated Mail with a Condition

#!/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
------------------

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.:frowning:

For starters with variable assignments, there should be no spaces around the =

IFILE=$HOME/bday.csv
OFILE=bday_out$$
MAILID=xxx@abc.com
1 Like
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

1 Like

Thanks for the response guys

  • scrutinizer - i made that correction after posting this here itself :slight_smile:
  • pravin27 i removed those lines but i am not able to execute the program it says

New 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

sreenadh,05,Thu,Dec,2013

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

Any suggestions?

Execute with shell options v and/or x set, then tell us the results of the lines

if [ $date -eq $DAT -a $day = $DAY -a $month = $MON ] 

and

if [ -f $OFILE -a -s $OFILE ]

. Don't \rm $OFILE but see if it exists after the run.

1 Like

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

Please use code tags as required by forum rules!

So - what are your conclusions from those messages?

Hi it means the if condition is not working correctly

can you try this

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

@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 ----------

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

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

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

It gives all the names as before

'IF' is not getting executed properly :frowning: donno Y!

try this

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

/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 # 


Looking at your execution log

+ [ 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!)

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.

#!/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! :frowning:

---------- 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 ----------

Tried this IF condition buddy, but still the result is same :frowning:
Is it because i am executing it in Ksh ? i cant do it in bash though!

Strange enough. Well, last resort: Run the entire script with set -vx and attach the entire execution log here.

Please find the log here

+ [[ 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

This is not the entire script's log, but it shows at least how the central conditional expression is looped through

+ [[ 05 -eq 05 ]]
+ [[ Thu = Thu ]]
+ [[ Dec = Dec ]]             # all three conditions TRUE --> print name
+ [[ 05 -eq 05 ]]
+ [[ Thu = Thu ]]
+ [[ December = Dec ]]        # third cond. failed, next loop iteration
+ [[ 06 -eq 05 ]]             # first cond. failed, skip rest, next iteration

So - there should be only one name printed to the output file.

u can go through the below link for more on if conditions hope u can find a way out

http://www.softpanorama.info/Scripting/Shellorama/Control\_structures/if_statements.shtml