Operating System - HP-UX
1824988 Members
2073 Online
109678 Solutions
New Discussion юеВ

Re: Can I get yesterday's date in a unix script?

 
SOLVED
Go to solution
A. Clay Stephenson
Acclaimed Contributor

Re: Can I get yesterday's date in a unix script?

This is simple...
simply type this:

DTE=$(date +%Y%m%d -1)
((Y=DTE - 1))
print $Y
20060902
print $DTE
20060903

Done...its as simple as that!

-----------------------------------

I completely agree this is a simple solution. Try it on the first day of the month (or better the first day of the year).
If it ain't broke, I can fix that.
Babu Yalamanchi
Advisor

Re: Can I get yesterday's date in a unix script?

A.C

I had a solution similar to Jody's. On
Sep 1 , it gave me yesterday's date 20060900. That's what sent me here in the first place. I played with your idea of passing a multiple of 86400 from Unix shell to Perl on command line. I am reasonably good in Shell but not in Perl. Although I settled on your caljd.sh, still I want to know how you pass a Shell variable to Perl.

Thanks
Babu Yalamanchi
Advisor

Re: Can I get yesterday's date in a unix script?

All

I initiated this question as a continuation of a thread started in 2004 because it relates to the original question AND the thread was not closed by the author. Now I am looking around to assign points to well deserved replies. But I can't. Does any body know how I can assign points to the replies to my question. Should I have started a new thread in the first place? I thought it will be easier explaining my question as a extension of an existing question.

Regards
James R. Ferguson
Acclaimed Contributor

Re: Can I get yesterday's date in a unix script?

Hi Babu:

Here's a simple Perl snippet that will find yesterday's date, or a day n-days ago:

# perl -le 'print scalar localtime(time-(86400*$ARGV[0]))' 2

...will return a date two (2) days ago. If you don't pass any argument, the current local date and time is returned.

You could pass a shell variable thusly:

# n=2;perl -le 'print scalar localtime(time-(86400*$ARGV[0]))' $n

...and if you want to use this to find future days:

# n=-2;perl -le 'print scalar localtime(time-(86400*$ARGV[0]))' -- $n

In the last case, note the '--' to signal that the negative value that follows is not a switch, but rather an argument.

Regards!

...JRF...
Regards!

...JRF...
Babu Yalamanchi
Advisor

Re: Can I get yesterday's date in a unix script?

JRF

Perfect. The '$n' is what I played with and failed. I now have two perfect solutions.

Thanks
jyothirmayee.jt
New Member

Re: Can I get yesterday's date in a unix script?

I have used the following ample number of times and it works for me

my $yesterday=`date -d 'yesterday' +%Y%m%d`

Jyothi