last month end date


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users last month end date
# 1  
Old 03-19-2008
last month end date

Hi,

How to get the lastmonth's end date? (have current date) through s shell script. I need to get it and pass it to a procedure.

Please advice.
Thanks in advance.
# 2  
Old 03-19-2008
check if you can find a solution here
# 3  
Old 03-21-2008
For future reference if someone reads this:
try cal
Code:
#!/bin/ksh
printf "%d %d" $(date "+%Y %m") | read year month
let month=$month-1
if [[ $month -eq 0 ]] ; then
   let year=$year-1
   let month=12
fi
cal $month $year | tr -s '\n' ' ' | awk '{print $NF}' | read day
printf "%d/%02d/%02d\n" $year $month $day


Last edited by jim mcnamara; 03-21-2008 at 02:01 PM.. Reason: changed month=1 to month=12
# 4  
Old 03-21-2008
Question Prior year, but month=1?

Code:
if [[ $month -eq 0 ]] ; then
   let year=$year-1
   let month=1
fi

Shouldn't this be ?
Code:
if [[ $month -eq 0 ]] ; then
   let year=$year-1
   let month=12
fi

January is month 1, so when you subtract 1 you get 0. And you rightly trap for it, decrementing the year but setting the month to 1 for January. I think you should set to 12 for December.
# 5  
Old 03-21-2008
Perl-based approach to deriving backwards in time...I haven't quite bothered to try going back to the future as of yet. But it also handles Leap Years...

Code:
$ pl_end_of_last_month_0=`perl -e '\
> $y= time - (86400 * (localtime(time))[3]); \
> printf "%04d%02d%02d\n", (localtime($y))[5] + 1900 ,(localtime($y))[4] + 1 ,(localtime($y))[3] ; ' ` 
$ echo $pl_end_of_last_month_0 
20070831
===

Code:
$ # Today... 
$ pl_today_0=`perl -e '\
> $y= time - (86400 * $ARGV[0]); \
> printf "%04d%02d%02d\n", (localtime($y))[5] + 1900 ,(localtime($y))[4] + 1 ,(localtime($y))[3] ; ' 0 ` 
$ echo $pl_today_0 
20070912
=== 
$ # Today minus 1... (um, yesterday...?) 
$ pl_today_1=`perl -e '\
> $y= time - (86400 * $ARGV[0]); \
> printf "%04d%02d%02d\n", (localtime($y))[5] + 1900 ,(localtime($y))[4] + 1 ,(localtime($y))[3] ; ' 1 ` 
$ echo $pl_today_1 
20070911
=== 
$ # Today minus a defined number... 
$ my_number=3 
$ pl_today_mynumber=`perl -e '\
> $y= time - (86400 * $ARGV[0]); \
> printf "%04d%02d%02d\n", (localtime($y))[5] + 1900 ,(localtime($y))[4] + 1 ,(localtime($y))[3] ; ' ${my_number} ` 
20070909
===

# 6  
Old 03-21-2008
Quote:
Originally Posted by joeyg
Code:
if [[ $month -eq 0 ]] ; then
   let year=$year-1
   let month=1
fi

Shouldn't this be ?
Code:
if [[ $month -eq 0 ]] ; then
   let year=$year-1
   let month=12
fi

January is month 1, so when you subtract 1 you get 0. And you rightly trap for it, decrementing the year but setting the month to 1 for January. I think you should set to 12 for December.
You are right. I was being bombed with other things when I did that... my bad.
# 7  
Old 03-21-2008
And perl/python/C is better solution.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How bash treats literal date value and retrieve year, month and date?

Hi, I am trying to add few (say 3 days) to sysdate using - date -d '+ 3 days' +%y%m%d and it works as expected. But how to add few (say 3 days) to a literal date value and how bash treats a literal value as a date. Can we say just like in ORACLE TO_DATE that my given literal date value... (2 Replies)
Discussion started by: pointers1234
2 Replies

2. Shell Programming and Scripting

Not able to fetch previous month first date and last date

I am not able to fetch first date and last date previous month date -d -1month +%Y-%m-%d date -d -1month +%Y-%m-%d I need two format dd-mm-yyy previous month 01-03-2016 previous month 31-03-2016 and also only date 1 to 31 Aprriciate your replay (4 Replies)
Discussion started by: jagu
4 Replies

3. UNIX for Dummies Questions & Answers

Get the next month end date

how to get the next month end date. dt=01312014 output=02282014 (2 Replies)
Discussion started by: w020637
2 Replies

4. Shell Programming and Scripting

Help with getting last date of previous month and first date of previous 4th month from current date

I have requirment to get last date of previous month and the first date of previous 4th month: Example: Current date: 20130320 (yyyymmdd) Last date of previous month: 20130228 (yyyymmdd) First date of previous 4th month: 20121101 (yyyymmdd) In my shell --date, -d, -v switches are not... (3 Replies)
Discussion started by: machomaddy
3 Replies

5. Shell Programming and Scripting

Cron for end of month

Hello, I'm on FreeBSD 8.2-STABLE shell and I was trying the following cron entry for end of month: 59 23 * * * && /home/user/eomt.sh >/dev/null 2>&1 I get the following error Subject: Cron <user@shell> eomt.sh contains the following codes #!/bin/sh rm /home/user/tst/logs/* &&... (9 Replies)
Discussion started by: user`
9 Replies

6. UNIX for Dummies Questions & Answers

print previous month (current month minus 1) with Solaris date and ksh

Hi folks month=`date +%m`gives current month Howto print previous month (current month minus 1) with Solaris date and ksh (7 Replies)
Discussion started by: slashdotweenie
7 Replies

7. Shell Programming and Scripting

Pass the first date and last date of previous month

Hi All, I need to run a job every month at the beginning of the month which is scheduled through autosys, lets say on 03/01/2010. I need to pass the last month's i.e February's first_date = 02/01/2010 and last_date = 02/28/2010 as variables to a stored procedure. Can somebody please pass... (2 Replies)
Discussion started by: vigdmab
2 Replies

8. Shell Programming and Scripting

fetching Month end using cal

Hi , Can someone please assist to get monthend using cal utility and monthend shouldn't be saturday or sunday. Cheers, gehlnar (2 Replies)
Discussion started by: gehlnar
2 Replies

9. Shell Programming and Scripting

End of Month - Cron Jobs

I am running AIX 5.3 with the default shell as ksh. I would like to know how to run a cron job on this system the last calendar day of the month. I found an example, but, I'm not sure if it will work with AIX and ksh... 02 16 * * * && /home/user/job-eom.kshI also need to be able to run a... (2 Replies)
Discussion started by: jthompson333
2 Replies

10. UNIX for Advanced & Expert Users

Unix Cron Month End Processing

Is it possible to use cron to schedule a job that will run the first 3 Sunday's during a month in addition to the last day of the month? (3 Replies)
Discussion started by: cpete
3 Replies
Login or Register to Ask a Question