Determine Yesterday’s Date in a unix environment using ksh and date()
Unix and KSH to find yesterday’s date
Mastering Unix Shell Scripting: Bash, Bourne, and Korn Shell Scripting for Programmers, System Administrators, and UNIX Gurus, 2nd Edition
Wicked Cool Shell Scripts: 101 Scripts for Linux, Mac Osx, and Unix Systems
I did a bunch of searching and found a lot of places online to do timestamp conversions, and perl programs to get yesterday’s date. Some worked and some didn’t. But none the less, I didn’t need a whole program to do this, and I especially didn’t need an online converter for use in my app. I needed something simple and small that didn’t use a lot of calulations.
Here is what i came up with for use in the shell (KSH)
# get todays timestamp in seconds and subtract
# a full days worth of seconds (86400) from it
datestamp=$( expr $( date +’%s’) – 86400 )
# then plug it back in to the date() command
YESTEDAY=$( date -d@${datestamp} +’%Y%m%d’ )
# this is formatted at YYYYMMDD
# man date() for formatting options
# do with it what you will
echo ${YESTERDAY}
Voila 2 lines of code.
Hi.
Thanks for the above lines of code.
Unfortunately, I wasn’t able to get it working on Sun Solaris 10 or MEPIS Linux 7.5.
Alternatively, I’ve got a one line code that requires PERL:
perl -e ‘print scalar(localtime(time – 86400)), “\n”‘
Thanks for the contribution. Which LINUX/UNIX did you run the above script on?
Kind Regards,
Jason
I used it on freebsd 6.0 and also ubuntu linux. I also had it working on a solaris workstation. Can’t remember which version though. Thanks for the perl one liner, I love PERL