- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: Last Day of Month?
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-09-2002 02:03 PM
тАО04-09-2002 02:03 PM
I need to be able to tell inside a script if the current date is the last day of the month. I also need it to work during leap years.
Any thoughts?
TIA, John
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-09-2002 02:07 PM
тАО04-09-2002 02:07 PM
SolutionThis sounds like a job for my universal date hammer, caljd.sh.
if [ "$(caljd.sh -M)" != "$(caljd.sh -n 1 -M)" ]
then
echo "Last Day of the Month"
fi
Basically, this compares today's month with that of tomorrow. If they are different then it must be the last day of the month. Invoke caljd.sh -u for full usage.
Regards, Clay
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-09-2002 02:31 PM
тАО04-09-2002 02:31 PM
Re: Last Day of Month?
Clay's caljd.sh is SUPER, there is another work around if you like ..
These are from my old notes, not tested ...
-----------------
The idea is to check whether
*tomorrow* is the first day of a month.
using date, change MET to your local time zone
#!/bin/ksh
if test `TZ=MET-24 date +%d` = 1; then
# today is the last day of month
print "Today is the last day of the month"
else
print "Today is NOT the last day of the month"
fi
-------------
Perl Method
#!/usr/bin/perl -w
#
# last-day-of-month - check if today is the last day of a month
#
# Input: none.
# Output: none.
# Exit status: 0 (true) if today is the last day in a month, otherwise 1.
# Algorithm: Get localtime and advance the day of month by one. Let mktime
# normalize the result and check whether day of month became 1.
# Requires: perl5.
#
use POSIX;
@the_time = localtime (time);
++$the_time[3]; # Element 4 is the day of month [1..31]
if ((localtime (POSIX::mktime (@the_time)))[3] == 1) {
exit 0;
}
exit 1;
------------
Hope this helps !
Thanks,
Shabu
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-09-2002 02:54 PM
тАО04-09-2002 02:54 PM
Re: Last Day of Month?
Also there is another trick that I just found out using the cal (print calendar) command
-------------
#!/usr/bin/ksh
LASTDAY=`cal | sed '/^$/d' | tail -1 | head -1 | awk '{print $NF}'`
echo $LASTDAY
CURR_DATE=`date +%d`
echo $CURR_DATE
if [[ "${CURR_DATE}" -eq "${LASTDAY}" ]]; then
print "Today is the last day of the month"
#do your stuff
else
print "Today is NOT the last day of the month"
fi
----------------
Thanks,
Shabu
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-09-2002 09:45 PM
тАО04-09-2002 09:45 PM
Re: Last Day of Month?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-10-2002 12:59 AM
тАО04-10-2002 12:59 AM
Re: Last Day of Month?
> do
> b=$i
> done; echo $b
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-10-2002 01:47 AM
тАО04-10-2002 01:47 AM
Re: Last Day of Month?
Regards,
Ceesjan
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-10-2002 03:41 AM
тАО04-10-2002 03:41 AM
Re: Last Day of Month?
Ewww! too many awks; waste of processing time... You'll be using cat next!
Why don't you just use the Record Seperator field:
cal | awk 'BEGIN { RS='\n'} {print $NF}'
Bit shorter :-)
dave
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-10-2002 04:00 AM
тАО04-10-2002 04:00 AM
Re: Last Day of Month?
...and here's one more 'awk' variation using 'cal':
# cal|awk 'NF>0 {D=$NF} END{print D}'
# cal 02 2000|awk 'NF>0 {D=$NF} END{print D}'
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-10-2002 04:13 AM
тАО04-10-2002 04:13 AM
Re: Last Day of Month?
You realy should learn perl and be flexible. Let's say I want to know if it's only less than 5 day's till the end of the month? Can you do that with $TZ, cal, caldj?
l1:/u/usr/merijn 103 > perl -le '$n=shift;(localtime time+$n*86400)[3]<=$n&&print"the end is near"' 20
l1:/u/usr/merijn 104 > perl -le '$n=shift;(localtime time+$n*86400)[3]<=$n&&print"the end is near"' 21
the end is near
l1:/u/usr/merijn 105 >
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-10-2002 08:01 AM
тАО04-10-2002 08:01 AM
Re: Last Day of Month?
Yes, I can do that in caljd.sh, substitute a variable for -n 1 and you are there. Of course, a more robust version would have to compare both the month and the year in case your $n got very large. I only wish I were smart enough to code in Perl.
Now, can you do this in one line?
What is the date 90 days from now unless that happens to fall on a weekend or a holiday? (In that case skip to the next non-excluded day).
Oh, and while you are at it, print the output in YYYY/MM/DD format. Believe it or not, there was a posting with almost that very request and it turned out to be a one-liner for caljd.sh.
DT=$(caljd.sh -y -S "/" $(caljd.sh -n 90 -x 0 -x 6 -H))
Oh well, maybe I should have tried to use my itty-bitty knowledge of Perl for that.
Regards, Clay
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-10-2002 02:57 PM
тАО04-10-2002 02:57 PM
Re: Last Day of Month?
I used A. Clay's method. It worked the first time and then I printed the usage message and found many useful options for that script.
Thanks everybody for the quick help. I'm sorry it took so long to assign points. I was having a very difficult time using the forum today.
John W.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-11-2002 04:48 AM
тАО04-11-2002 04:48 AM
Re: Last Day of Month?
I know nothing about holidays, and I'm still not using a module. It's still one line of standard perl
l1:/tmp 118 > perl -le '$d=86400;$t=time+$d*shift;$t+=$d while(!((localtime$t)[6]%6));print scalar localtime$t;' 90
Wed Jul 10 14:52:14 2002
l1:/tmp 119 > perl -le '$d=86400;$t=time+$d*shift;$t+=$d while(!((localtime$t)[6]%6));print scalar localtime$t;' 91
Thu Jul 11 14:52:18 2002
l1:/tmp 120 > perl -le '$d=86400;$t=time+$d*shift;$t+=$d while(!((localtime$t)[6]%6));print scalar localtime$t;' 92
Fri Jul 12 14:52:20 2002
l1:/tmp 121 > perl -le '$d=86400;$t=time+$d*shift;$t+=$d while(!((localtime$t)[6]%6));print scalar localtime$t;' 93
Mon Jul 15 14:52:23 2002
l1:/tmp 122 > perl -le '$d=86400;$t=time+$d*shift;$t+=$d while(!((localtime$t)[6]%6));print scalar localtime$t;' 94
Mon Jul 15 14:52:25 2002
l1:/tmp 123 > perl -le '$d=86400;$t=time+$d*shift;$t+=$d while(!((localtime$t)[6]%6));print scalar localtime$t;' 95
Mon Jul 15 14:52:28 2002
l1:/tmp 124 > perl -le '$d=86400;$t=time+$d*shift;$t+=$d while(!((localtime$t)[6]%6));print scalar localtime$t;' 96
Tue Jul 16 14:52:30 2002
l1:/tmp 125 >
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-11-2002 05:42 AM
тАО04-11-2002 05:42 AM
Re: Last Day of Month?
#!/usr/bin/sh
# Report the numeric day of the last day
# of a particular month and year
# Usage: lastday [ month-number [ year ] ]
set -u
PATH=/usr/bin
# Setup defaults
MYNAME=${0##*/}
MONTH=$(date "+%m")
YEAR=$(date "+%Y")
# Process optional arguments
if [ $# -gt 0 ]
then
if [ $# -eq 1 ]
then
MONTH=$1
else
MONTH=$1
YEAR=$2
if [ ${#YEAR} -eq 2 ]
then
YEAR=$(( YEAR + 2000))
fi
fi
fi
# Find the last day of the month using cal
CALDAYS=$(cal $MONTH $YEAR 2> /dev/null)
if [ $? -ne 0 ]
then
echo "Bad Argument(s): $@"
echo "Usage: $MYNAME [ month [ year ] ]"
exit 1
else
LASTDAY=$(echo "$CALDAYS" | awk 'BEGIN { RS='\n'} {print $NF}' )
echo "For $MONTH/$YEAR, last day is $LASTDAY"
fi
Bill Hassell, sysadmin