Operating System - Linux
1745801 Members
3429 Online
108722 Solutions
New Discussion юеВ

Re: Simple way of obtaining previous day?

 
david swinnerton
New Member

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 5
Geoff Wild
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.
James R. Ferguson
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...
Pete Randall
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
Dennis Handly
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.
david swinnerton
New Member

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.