Anyone know how to calculate “yesterday” in a shell script using ‘date’? I’ve looked at BSD man pages and Google can’t get anything to work. This is what I am using now:
do shell script "date '+%m/%d/%y' | sed -e 's/000$//' -e 's/^0//'")
and I’d like that to return yesterday instead of the current day. I know how to get “yesterday” in PHP,
(do shell script "/usr/bin/php -r 'echo date(\"n/d/y\", strtotime(\"-1 day\"));'")
-- Use shell arithmetic expansion coupled with the date command's abilty to output seconds-since-epoch as well as generate output for dates given as second-since-epoch.
-- The sed bit was copied from the original post (the first sed expression in the OP was pointless; this uses only the second).
-- Since we are only extracting the day, this should be fine even around "daylight savings" days and times. If you needed hours, you would have to deal with "24 hours before now" sometimes not being the same hour of the day as "now".
do shell script "date -r $(( $(date +%s) - 24 * 60 * 60 )) +%m/%d/%y | sed -e s/^0//"
Thanks for both the answers; I learned something with both because I’m pretty new to both shell scripts, sed and PHP. DST isn’t that big a deal as the script isn’t critical and I glance at the resulting date of the file name after I run the script. - Mark
The -v option seems to do what you want.
May be worth looking at the Man page to see all it quirks for the -v , since it is quite long section on it for a man page!