tags:

views:

1080

answers:

3

I realize I could whip up a little C or Ruby program to do this, but I want my script to have as few dependencies as possible.

Given that caveat, how does one do date math in a bash script on OS X? I've seen a post (on another site) where someone did the following:

date -d "-1 day"

But this does not seem to work on OS X.

Addendum:

Several people have commented and responded that Ruby, Python, Perl, and the like come standard with OS X. I'm familiar with all three of these languages and could easily write a script that does what I want. As a matter of fact, I already have such a script, written in Ruby.

So perhaps I should clarify what I mean by 'external dependency'. What I mean is, I don't want my bash script to have to call any other script external to it. In other words, I want it to use some utility available in a vanilla installation of OS X and already on the path.

However, it doesn't look like this is possible, so I will have to make due with my external dependency: a Ruby script.

+3  A: 
$ date -v -1d

-d sets Daylight Savings time flag.

Try man date for more info.

Matthew Schinckel
+1  A: 

The command Matthew mentions works for me with /bin/date on Leopard. Besides, as Schwern points out, if you're sure your shell script is going to be run on Mac OS X, why don't you want to use Python or Ruby, that both come in standard in /usr/bin? This would in no way affect portability in your particular scenario.

Arthur Reutenauer
A: 

I realize I could whip up a little C or Ruby program to do this, but I want my script to have as few dependencies as possible.

Doing in C or as a Ruby script would result in less dependancies, and be more cross-platform.. Bash-scripting just calls various other scripts/applications, mainly the GNU utilities.. Many of the commands are unavailable, or take different arguments on other platforms (especially between OS X and Linux)..

Ruby is included by default on OS X Leopard (and 10.4, and probably previous versions if I recall correctly), and if you use Rubys built-in date libraries, it will work consistently on any platform.

Whenever you try to do anything remotely complicated, you are generally best of using a "proper" scripting language!

dbr