Posts

Showing posts from September, 2010

Trick and Tips when bash script dealing date

This is some tricks and tips I found when I searching around google.

Getting Yesterdays or Tomorrows day with shell date command.

When invoked without arguments, the date command displays the current date and time. Depending on the options specified, date will set the date and time or print it in a user defined way. I’ve seen many people writing a perl script for calculating yesterday or tomorrow. Computer loves numbers but we love relative terms like 2 days ago. Luckily GNU date command is designed to handle relative date calculation.

Why use relative date formats?

* Ease of use
* To write your own scripts
* Automate task using cron (example run a job on last day of the month or Nth day of the month or 3rd Friday and so on)

First, print today's date:
$ date
Sun Jun 17 12:17:24 CDT 2007

Now display Yesterday's date:
$ date --date="1 days ago"
OR try:
$ date --date="yesterday"
Sat Jun 16 12:17:20 CDT 2007

Now display Tomorrow's date:
$ date --date="-1 d…