This will be minimized in a shell script. You are better off using Date :: Manip in perl or something similar in another full-featured language. However, I can come up with some ways to do this with the date command. First of all, you can use the -date parameter to set the starting point for the date, for example:
$ date --date = '08 -JUN-2011 '
Wed Jun 8 00:00:00 EDT 2011
You can get the previous date as follows:
$ date --date = '08 -JUN-2011 -1 days'
Tue Jun 7 00:00:00 EDT 2011
On the last day of the month, I just return from 31 to the date, not failing. Can you check $? for this
$ date --date = '31 -JUN-2011 '; echo $?
date: invalid date `31-JUN-2011 '
one
$ date --date = '30 -JUN-2011 '; echo $?
Thu Jun 30 00:00:00 EDT 2011
0
On the first day of the month ... it's usually 01 :)
frankc
source share