determine last day of the month


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting determine last day of the month
# 1  
Old 06-02-2009
determine last day of the month

In a ksh script how do I determine if I am on the last day fo the month ?

thanks
# 2  
Old 06-02-2009
Hammer & Screwdriver Here is a thought-process

a) determine today's date as time-since-epoch; something like 1227894736
b) figure one days worth of seconds; 24*60*60
c) tomorrow=today+(24*60*60)
d) see what that day is; a command like the following
echo $(date '+%Y-%m-%d' -d @$tomorrow)
which will give you tomorrow's date as 2009-06-03

Try to experiment a little with that.
# 3  
Old 06-02-2009
Code:
#!/bin/ksh

isleapyr()
{
        year=$1
        if [[ $(( $year % 4 )) -eq  0 && $(( $year % 100 )) -ne 0  \
                          || $(( $year % 400 )) -eq 0 ]] ; then     
           echo 1                                                   
        else                          
           echo 0
        fi
}

days_in_a_month()
{
        month=$(( $1 - 1 ))
        start=0
        result=0

        if [[ $( isleapyr $2 ) -eq 1 ]] ; then
                set -A arr 31 29 31 30 31 30 31 31 30 31 30 31
        else
                set -A arr 31 28 31 30 31 30 31 31 30 31 30 31
        fi
        export mxdays=${arr[month]}
        
												
}
typeset -i thismonth
typeset -i today
typeset -i thisyear
date +"%d %m %Y" | read today thismonth thisyear
days_in_a_month $thismonth $thisyear 
if [[ $mxdays -eq $today ]] ; then
   echo "last DOM"
else
   echo "not last DOM"
fi

try something like this. Note that some shells like ksh93 and the GNU date command will also do date arithmetic for you. This assumes just ksh
# 4  
Old 06-02-2009
Code:
#!/bin/ksh

today=$(date +%d)
lastDay=$(echo $(cal) | awk '{print $NF}')

if [ "${today}" = "${lastDay}" ]; then
   echo 'Today is the LAST day of the month'
fi

# 5  
Old 06-04-2009
Quote:
Originally Posted by vgersh99
Code:
#!/bin/ksh

today=$(date +%d)
lastDay=$(echo $(cal) | awk '{print $NF}')

if [ "${today}" = "${lastDay}" ]; then
   echo 'Today is the LAST day of the month'
fi


Thanks!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How can I get first day of a particular month?

Hi, I am new to shell scripting and I have a requirement of getting first day of specific month. eg i need 1st day of oct 2014 October 2014 S M Tu W Th F S 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 output... (4 Replies)
Discussion started by: simsim90
4 Replies

2. Shell Programming and Scripting

Julian day to dates in YEAR-MONTH-DAY

hello, I have many files called day001, day002, day003 and I want to rename them by day20070101, day20070102, etc. I need to do it for several years and leap years as well. What is the best way to do it ? Thank you. (1 Reply)
Discussion started by: Ggg
1 Replies

3. Shell Programming and Scripting

Script to counting a specific word in a logfile on each day of this month, last month etc

Hello All, I am trying to come up with a shell script to count a specific word in a logfile on each day of this month, last month and the month before. I need to produce this report and email it to customer. Any ideas would be appreciated! (5 Replies)
Discussion started by: pnara2
5 Replies

4. Shell Programming and Scripting

GMT -4 to determine day of week

I pulled the following code from one of Jim's threads. Since my server time is GMT, I need to make sure the output is GMT-4. Does anyone know how to tweak Perl to reflect this? Thanks Scott #!/bin/ksh # input format YYYY-MM-DD prints day name dow() { perl -e ' use POSIX... (13 Replies)
Discussion started by: WhoDatWhoDer
13 Replies

5. Shell Programming and Scripting

how can i get the first day of the pre month

how can i get the first day of the pre month in Bash (3 Replies)
Discussion started by: qjlongs
3 Replies

6. Shell Programming and Scripting

Code creates day 32 instead of 1st day of next month.

I am using the code below modified from a post I saw here regarding having the script write out future dates. The problem is that instead of making 8/1 it makes 7/32! Please help! yy=`date +%Y` mm=`date +%m` dd=`date +%d` echo "Today is : $yy $mm $dd" #!/usr/bin/ksh date '+%m... (5 Replies)
Discussion started by: libertyforall
5 Replies

7. Shell Programming and Scripting

Script to find previous month last day minus one day timestamp

Hi All, I need to find the previous month last day minus one day, using shell script. Can you guys help me to do this. My Requirment is as below: Input for me will be 2000909(YYYYMM) I need the previous months last day minus 1 day timestamp. That is i need 2000908 months last day minus ... (3 Replies)
Discussion started by: girish.raos
3 Replies

8. Shell Programming and Scripting

The last day of the last month

I am a beginner in unix. Can anyone tell me how to get the last day of the last month in 'yyyymmdd' format? eg. today is 20050909, I want to get 20050831. Thanks in advance. (2 Replies)
Discussion started by: emily79
2 Replies

9. Shell Programming and Scripting

last day of the month

can any one please tell me how can I get the last day of the month Thanks (4 Replies)
Discussion started by: aya_r
4 Replies

10. Shell Programming and Scripting

Write a shell script to find whether the first day of the month is a working day

Hi , I am relatively new to unix... Can u pls help me out to find out if the first day of the month is a working day ie from (Monday to Friday)...using Date and If clause in Korn shell.. This is very urgent. Thanks for ur help... (7 Replies)
Discussion started by: phani
7 Replies
Login or Register to Ask a Question