Languages and Scripting
Showing results for 
Search instead for 
Do you mean 

Simple way of obtaining previous day?

Occasional Visitor

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"?
5 REPLIES
Honored Contributor Honored Contributor

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 Acclaimed Contributor

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...
Highlighted
Outstanding Contributor Outstanding Contributor

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 Acclaimed Contributor

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

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.