#!/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.
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
scrutinizer - i made that correction after posting this here itself
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.
+ [ 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!
---------- 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
Is it because i am executing it in Ksh ? i cant do it in bash though!