This one just blew me away: I spent a while trying to figure out how to turn this month’s date into last month’s date in a Bash script. In the script (okay, it drives AWStats reporting) I want to make a summary file for last month. To do this I want to check if a file exists with last month in it’s name and if it doesn’t exist I want to create it.
I started with the date command. This is a quick way to get the current date and time at the command prompt in Linux, just type “date”. The date command also takes formatting parameters with an option like +%Y-m, so the command
rob@ruby:~> date +%Y-%m
Predictably answers “2006-08″ for me today. To get last month’s date I assumed I’d have to work with that last number, subtract one, accomodate the zero padding, yada yada yada. Yuck. Nonetheless I set to work and had a little success with turning a 08 into a 7 with expr and some backticks like expr `date +%m` - 1.
I figured I’d stuff something back into date --date="$f" with $f as my calculated date from last month. Then I started looking at the --date parameter in the man and info pages for it. All it says is
-d, –date=STRING
display time described by STRING, not `now’
So I tried “now” for STRING and sure enough it was the same as no --date parameter. Interesting. How about yesterday? Does date believe in yesterday?
rob@ruby:> date --date="yesterday" +%Y-%m-%d
2006-08-16
It sure does. So anyways, here’s the one that really blew me away (and solves my problem):
rob@ruby:> date --date="last month" +%Y-%m
2006-07
Knowing that the string can be so complex I googled it just now and found this date reference (which looks like a much more detailed man page than the one in my current SuSE 10.1 install). Specifically, it says that
DATESTR can be in almost any common format. It can contain month names, timezones, ‘am’ and ‘pm’, ‘yesterday’, ‘ago’, ‘next’, etc.
And there are also many good examples of usage. So I guess I need a better manual installed, but then I suppose that would’ve spared me the joy of guessing the right answer.