Solved Trying to test for the end of the month in a script.

Red hat Software red hat ent linux ws ba...
April 26, 2012 at 10:00:59
Specs: Linux, 3GHz/4GB
How do I test for end of the month in a korn shell script?

If today is the end of the month run the script
If today is not the end of the month write it to the log file and exit
Echo Today is not the end of the month>>$LOG


See More: Trying to test for the end of the month in a script.

Report •

#1
April 26, 2012 at 11:31:11
A way is to parse the output of the Unix calendar (cal) program to determine the end of the current month and then check if today is the end of the current month:

#!/bin/ksh

LOG=./mylog.txt
m=$(date '+%m') # get the month
d=$(date '+%d') # get the day
y=$(date '+%Y') # get the year

# find the end day of the month using the unix cal command:
endday=$(cal $m $y|awk ' { print $NF } '| sed '/^$/d' |tail -1)

if [[ $endday == $d ]]
then
    echo "run your script"
else
   echo "$d is not the end of the month" >> $LOG
fi


Report •

#2
April 26, 2012 at 13:38:31
✔ Best Answer
Thank you for your help! I decided to use this method after researching my options.

# Check to see if it is the end of the month.
CHECKEOM=`date --d=tomorrow +%d`
if [ "$CHECKEOM" -eq 01 ]; then
echo "\nThis script is running today.">>$LOG
else
echo "\nThis script only runs at the end of the month.">>$LOG
exit
fi


Report •

#3
April 26, 2012 at 14:01:56
That is a nice script. Keep in mind that the -d option is a GNU extension to the date command meaning it will run on Linux, but not on the legacy systems like Solaris, AIX, HP-UX, etc.


Report •
Related Solutions


Ask Question