Image Ulrich Habel
About me/Impressum

NetBSD

search

Google


Search WWW
Search rhaen daily

archives

categories

feeds

Perl nation banner logo thing
09/21/2008

How do I calculate yesterdays date for shell scripts?

Well, you don't. I encountered this problem several times in my career as Unix admin and usually the answer is: you don't. Just use a different timezone and you are set. Let me explain the problem. Usually you want a cronjob to do work like grepping through logfiles with the date of the day before. This ends up in a horrible mix of expr and awks to find out the day yesterday. An even worse thing is to use perl to get the date the far worst thing is to install GNU date for this. Just look at the following example:

rhaen@wiesel.pkgbox.org:rhaen $ echo $(date)
Sun Sep 21 12:03:20 CEST 2008
rhaen@wiesel.pkgbox.org:rhaen $ echo $(TZ=CEST23CEST date)  
Sat Sep 20 12:06:50 CEST 2008
Explanation: We use the environment variable TZ (timezone) to set a timezone which is 23 hours before our current timezone. As we don't use EXPORT to set the timezone the environment is changed just for the only command. This is an easy way to get 23 hours back, plenty of time for your cronjob needs. The same method works on every timezone, of course.

References

written by: Ulrich Habel (rhaen)

[/NetBSD] [permanent link]

powered by NetBSD