Operating System - HP-UX
1758844 Members
3151 Online
108876 Solutions
New Discussion юеВ

Shell Script - Yesterday's date

 
SOLVED
Go to solution
Tracey
Trusted Contributor

Shell Script - Yesterday's date

Could someone please share with me the easiest way to find out yesterday's date (today-1)in a shell script?

Thanks,
Tracey
9 REPLIES 9
Alan Riggs
Honored Contributor
Solution

Re: Shell Script - Yesterday's date

TZ=GMT+34 date

You will have to play with the time offset to match your particular timezone.
harry d brown jr
Honored Contributor

Re: Shell Script - Yesterday's date

Alan Riggs
Honored Contributor

Re: Shell Script - Yesterday's date

oops:

typo 34 should be 23
A. Clay Stephenson
Acclaimed Contributor

Re: Shell Script - Yesterday's date

Hi Tracey:

Try this:
YESTERDAY=$(caljd.sh $(caljd.sh -p 1))
echo "Yesterday = ${YESTERDAY}"

Use the attached caljd.sh

Regards, Clay
If it ain't broke, I can fix that.
Mike Hassell
Respected Contributor

Re: Shell Script - Yesterday's date

Tracey,

Or you can take the simple route:

#!/bin/sh

today=`date +%d`

yesterday=`expr $today - 1`

echo $yesterday

Of course this may not be as efficient as the others listed above.

-Mike
The network is the computer, yeah I stole it from Sun, so what?
Craig Rants
Honored Contributor

Re: Shell Script - Yesterday's date

Try this out

DATE=`TZ=MET+24 date +%d`
echo $DATE

Good Luck,
C
"In theory, there is no difference between theory and practice. But, in practice, there is. " Jan L.A. van de Snepscheut
Jeffrey S. Sims
Trusted Contributor

Re: Shell Script - Yesterday's date

In perl you can try

perl -e 'print scalar localtime (time -1*86400),"\n"' and you can replace the 1 with whatever number you want if you need more than one day ago.

Hope it helps
Craig Rants
Honored Contributor

Re: Shell Script - Yesterday's date

Tracey,
Thanks for the points, you pushed me over the top and presented me with what everyone says is the coolest hat to have. Thanx.

C
"In theory, there is no difference between theory and practice. But, in practice, there is. " Jan L.A. van de Snepscheut
Dave La Mar
Honored Contributor

Re: Shell Script - Yesterday's date

Another take on Mike's -

let yesterday=`date +%d`-1
dl
"I'm not dumb. I just have a command of thoroughly useless information."