Simple way of obtaining previous day? (290 Views)
Reply
Occasional Visitor
david swinnerton
Posts: 2
Registered: ‎01-02-2004
Message 1 of 6 (290 Views)

Simple way of obtaining previous day?

I need to query a backup log each day that has the name of the previous day. eg on tuesday the script will need to query backup.log.Mon and so on. date +%a would give an output of "Tue" but is there an easy way to produce the previous day of "Mon"?
Honored Contributor
Geoff Wild
Posts: 7,170
Registered: ‎06-04-2001
Message 2 of 6 (290 Views)

Re: Simple way of obtaining previous day?

perl -le 'print scalar localtime time - 86400' |awk '{print $1}'


Rgds...Geoff
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 3 of 6 (290 Views)

Re: Simple way of obtaining previous day?

Hi David:

# perl -MPOSIX -le 'print strftime "%a",localtime(time-(60*60*24))'

This subtracts one day (in seconds) and gives the previous day. The 'strftime' formatting directives are the same as those you would use with 'date'.

Regards!

...JRF...
Outstanding Contributor
Pete Randall
Posts: 16,205
Registered: ‎11-03-1996
Message 4 of 6 (290 Views)

Re: Simple way of obtaining previous day?

A simple case statement would suffice:

for DAY in `date +%a`
do
case $DAY in
Sun) PREVDAY=Sat
;;
Mon) PREVDAY=Sun
;;
Tue) PREVDAY=Mon
;;
Wed) PREVDAY=Tue
;;
Thu) PREVDAY=Wed
;;
Fri) PREVDAY=Thu
;;
Sat) PREVDAY=Fri
;;R=$PRNTR
*) PREVDAY=Oops
;;


Simplistic, but functional.


Pete

Pete
Acclaimed Contributor
Dennis Handly
Posts: 25,290
Registered: ‎03-06-2006
Message 5 of 6 (290 Views)

Re: Simple way of obtaining previous day?

One way of doing this is to create a link that points to the current day at the end of today's processing. That way, tomorrow, it will be the previous day.

Of course, you need to fix the boundary value problem by pointing to some empty file for the first time?

Or you can look for Clay's caljd.pl script:
http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=1126040

And many other threads.
Occasional Visitor
david swinnerton
Posts: 2
Registered: ‎01-02-2004
Message 6 of 6 (290 Views)

Re: Simple way of obtaining previous day?

Hi, thanks for the quick replies. The way I initially thought was to do a case statement but knew there must be a far shorter way of doing it. I have used Geoffs suggestion. cheers.
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.