• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 4551
  • Last Modified:

How do I get previous months start and end dates in Unix shell script?

Hi,

From any given date in a month I am wanting to work out the start and end dates of the previous month.

For example, in a unix shell script that I am running anytime in November I want to end up with the following two values :

start_date = "20071001"
end_date = "20071031"

Is this possible?

Cheers

Jo
0
JoHarris
Asked:
JoHarris
1 Solution
 
TintinCommented:
If you have GNU date (you didn't mention what Unix flavour you are using), then the following will work

month=`date --date "last month" +%m`
year=`date --date "last month" +%Y`
lastday=`cal $month $year|grep [0-9]|tail -1|awk '{print $NF}'`
start_date=$year${month}01
finish_date=$year$month$lastday


0
 
JoHarrisAuthor Commented:
sorry - should have mentioned which unix flavour ... thats worked an absolute treat though, many thanks!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Tackle projects and never again get stuck behind a technical roadblock.
Join Now