Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Determine if it is Last Day of the Current Month 1

Status
Not open for further replies.

Michael42

Programmer
Oct 8, 2001
1,454
US
Hello,

In a Bourne shell script on Solaris 8 I need to determine the last day of the current month. I need to use this in a daily cron.

If this is true (it is the last day of the current month) I want to perform a set of commands.

What can you suggest?

Thanks,

Michael42
 
This should work on almost all *nix versions

a=`date|awk '{print $2}'`
b=`TZ=GMT+24 date|awk '{print $2}'`

if [ $a = $b ] ; then
echo "Month same Do nothing"
else
echo "Month different Do something"
fi

Mike

"A foolproof method for sculpting an elephant: first, get a huge block of marble, then you chip away everything that doesn't look like an elephant."

 
feherke, the -d tomorrow option of the date command is a pure GNU stuff.
 
A small improvement to mrn's solution:

Code:
a=`date +%m` 
b=`TZ=GMT+24 date +%m`
...

Annihilannic.
 
b=`TZ=GMT+24 date +%m`
Probably good enough for britts but not so reliable for aussies ...
Have a look at the FAQ I mentioned above for better handling of time zone.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Guys,

Your suggestions rock as usual. I must share this with you :
Code:
 # get current day, month and year
   day=`date '+%d'`
   month=`date '+%m'`
   year=`date '+%Y'`

   # work out number of days in this month
   daysinmonth=`cal $month $year | awk '{ print $NF }' | grep -v "^$" |
tail -n 1`

   # if not last day of month exit now
   if [ "$day" != "$daysinmonth" ]
   then
     exit 0
   fi

Thanks,

Michael42
 
Nice solution. You can skip the grep and tail though by just doing:

Code:
daysinmonth=`cal $month $year | awk '!/^$/{last=$NF}END{print last}'`

Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top