Wednesday, November 16, 2005

Get the Date of Tomorrow/Yesterday in Shell Script

In shell script, we may need to get the date of tomorrow/yesterday. If you use GNU version of "date", it is a piece of cake:
date -d tomorrow +%Y-%m-%d
The above command will display the date of tomorrow in "yyyy-mm-dd" format. Similarly, to find the date of yesterday:
date -d yesterday +%Y-%m-%d
The power of the GNU date command is out of my imagination, see the following examples:
date -d "2 days ago"
date -d "3 weeks ago"
date -d "1 month ago"
date -d "3 days"
date -d "4 weeks"
date -d "2 month"
date -d "2 month 1 week"
However, if your "date" is non-GNU version (e.g. in Solaris), it doesn't support "-d" option. Then, there seems no simple command can do this. You may need to write Perl script or use other more powerful programming/scripting language to achieve same results.

No comments: