Well, let's do a short travel to the bottom of unix-date (gnu-date?) and back, avoiding perl, 'cause, unreadable code can be done without perl, can't it?
Lesson 1: Format the date with +..., + means 'here comes format' %Y and so on might be looked up with date --help immediately:
That was quiete simple, wasn't it? Visit us tomorrow for the next lesson!
Ok. Lesson 2:
Don't be fixed to today, let's jump directly one month back:
You see us repeating lesson 1 with the format, but -d means 'take a date I provide', and then you put in your date, or you do some arithmetic on the current date. -1month is pretty self explantory, but the missing blank is hard to remember.
Therefore we take a little brake and see us tomorrow.
Ok. Here we go. Lesson 3:
We take a small excursus to string-manipulation in bash.
The output of cmd CMD can be put to a variable cmd with cmd=$(CMD)
first is 8 characters starting at index 0 and we put 01 to the end. We could have used:
but we need lastm though.
Now that was a hard lesson - let's have a brake!
Lesson 4 (allways the hardest one):
Not really that hard, wasn't it? Take first as a date, add 1 month, add 1 negative day, fomat it all the way. Cut off 2010-03- (8 characters) in a second step.
Okay - too much effort. We could use this instead:
This was the only useful thing in all those commands, because it will handle 31 or 30 days, as well as leap years and exceptions from leap years and so on. Ok - Month 01-1=12 was useful before.
Now we got what we need, and put it all together, tomorrow...
Lesson 5: Earning the fruit:
Wow. Now put everything in one line, and you'll got perl.
Some goodies for the weekend:
For more details see:
Our solution was, put to one block of code: