Jump to content

BASH: Determine if last day of month

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
1 reply to this topic

#1
Guest_Jordan_*

Guest_Jordan_*
  • Guests
An easy way to determine if today is the last day of the month is to determine the numerical date for tomorrow. If tomorrow is "1" then you know today is the last day of the month. This holds true for months with 31 days, 30 days or for Feb. with 28.

#!/bin/bash

MYTZ=`date | awk '{print $5}'`
TOM=$(TZ=$MYTZ-24 date +%d)

if [ $TOM == 1 ]
 then
        echo "Last Day of the month!"
        # Enter your monthly code here
fi

This will allow you to create a script that executes on a monthly basis.

#2
Tor

Tor

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 486 posts
You could also use the /etc/cron.monthly and it will execute at the first of the month (or last, can't remember which) without you having to code for it.