APLawrence.com -  Resources for Unix and Linux Systems, Bloggers and the self-employed

Yesterday

Recently I was asked how to write a script that would automate the task of copying a log file to another directory using a format that was indicated by the following example:

Say today is Thursday August 19. We want to copy the log to the directory /oldlogs/1999/Aug/Wed18 (it's yesterday's log we are copying).

In Perl or C, that kind of date manipulation is fairly easy, but the person who requested this wanted a shell script that he could easily understand.After some thought, I came up with this:




#!/bin/ksh
#/usr/local/bin/yesterday
set -A DAYS Sat Sun Mon Tue Wed Thu Fri Sat
set -A MONTHS Dec Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
YESTERDAY=$((`date +%d` -1))
MONTH=`date +%m`
YEAR=`date +%Y`
NDAY=`date +%u`
WEEKDAY=${DAYS[`date +%u`]}
if [ $YESTERDAY -eq "0" ]
then
        MONTH=$((MONTH-1))
        if [ $MONTH -eq "0" ]
        then
                MONTH=12
                YEAR=$((YEAR-1))
        fi
set `cal $MONTH $YEAR`
shift $(($# - 1))
YESTERDAY=$1
fi
TMONTH=${MONTHS[MONTH]}
# uncomment next line for debugging
# echo $WEEKDAY $YESTERDAY $MONTH $TMONTH $YEAR
mkdir -p /oldlogs/$YEAR/$TMONTH 2>/dev/null
cp log.old /oldlogs/$YEAR/$TMONTH/$WEEKDAY$YESTERDAY
 

This works correctly for any day, automatically taking leap years into account -assuming your version of "cal" handles leap years correctly!

One thing that needs explanation is the DAYS array: the first "Sat" entry never will be seen, because the lowest value that "date +%u" will return is "1" for Monday. The purpose of sticking Saturday in is to push the value down a notch, so that ${DAYS[1]} is Sunday- one lower than what it really is. The first "Dec" in the MONTHS array serves a similar purpose.

The "set `cal $MONTH $YEAR`" has the happy effect of setting the positonal parameters so that the last parameter is the numerical last day of the previous month. Shifting the positional parameters down by their number ($#) minus one leaves the day in $1. This little trick could have also been done using a ksh array, but it just seems more straightforward to take advantage of the builtin shift operator.

You can also make a more generic script I call "when":

#!/bin/ksh
# /usr/local/bin/when
# usage "when 7" to go back 7 days, "when 14" to go back 14, etc.
# useless to go back more than a month
set -A MONTHS Dec Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
BACK=$1
THEN=$((`date +%d` - $BACK))
MONTH=`date +%m`
YEAR=`date +%Y`
if [ $THEN -le "0" ]
then
        MONTH=$((MONTH-1))
        if [ $MONTH -eq "0" ]
        then
                MONTH=12
                YEAR=$((YEAR-1))
        fi
set `cal $MONTH $YEAR`
SHIFT=$(( $THEN * -1 + 1 ))
shift $(($# - $SHIFT))
THEN=$1
fi
TMONTH=${MONTHS[MONTH]}
# uncomment next line for debugging
echo  $THEN $MONTH $TMONTH $YEAR
 

See Yesterday's date in Korn shell and Auto Edge archive removal script also.


Got something to add? Send me email.


14 comments



Increase ad revenue 50-250% with Ezoic


More Articles by

Find me on Google+

© Tony Lawrence




---January 10, 2005

Thank you very much. I have been searching for finding previous date from the system date base and this solution fits me well.

Ragothaman. P






Thu Mar 24 19:09:07 2005: 232   TonyLawrence

gravatar

date --date='1 day ago'

is easier..



Thu May 5 13:09:47 2005: 465   Indy


perl -le 'print scalar localtime time - 86400'
YESTERDAY=`TZ=aaa24 date +Ym`
date -d yesterday

Formatting yesterday's date using AWK and date command
------------------------------------------------------
date -d yesterday +Yd
date -d yesterday | awk '{print $3}'






Thu May 5 13:11:07 2005: 466   anonymous


perl -le 'print scalar localtime time - 86400'
YESTERDAY=`TZ=aaa24 date +Ym`
date -d yesterday

Formatting yesterday's date using AWK and date command
date -d yesterday +Yd
date -d yesterday | awk '{print $3}'






Tue Aug 2 14:08:45 2005: 925   anonymous


Thanks, just a little bit detail... add a '0' when month or day are < 10.

if [ $YESTERDAY -eq "0" ]
then
MONTH=$((MONTH-1))
if [ $MONTH -eq "0" ]
then
MONTH=12
YEAR=$((YEAR-1))
fi

if [ $MONTH -le 10 ]
then
MONTH='0'$MONTH
fi

YESTERDAY=$1
fi

if [ $YESTERDAY -le 10 ]
then
YESTERDAY='0'$YESTERDAY
fi



Thu Sep 8 02:55:28 2005: 1063   anonymous


Thank you - it works!



Tue Mar 21 11:43:39 2006: 1802   TonyLawrence

gravatar
See (link) also.

There are dozens of ways to skin this cat.



Wed Oct 4 04:27:49 2006: 2497   anonymous


yest
(sourceforge.net/projects/yest)



Fri Oct 24 05:20:06 2008: 4682   anonymous


Thanks I needed this....



Wed Dec 10 19:17:20 2008: 4896   Jack


that's great and useful. But I need yesterday to be 09 if it is less than ten. I would normally use perl and the Date::Manip , but this system has me limited to ksh.

Any suggestions?








Wed Dec 10 19:19:41 2008: 4897   TonyLawrence

gravatar
Already mentioned in the comments above yours:

if [ $MONTH -le 10 ]
then
MONTH='0'$MONTH
fi



Wed Feb 4 08:44:12 2009: 5297   anonymous

gravatar
how to minus 1 month instead of minus 1 day??



Wed Feb 4 12:26:23 2009: 5300   TonyLawrence

gravatar
There's a harder problem. For one thing, what does it mean? If today is March 30th, is a month ago February 28th? Or is it the 26th? Or something else? What are you actually trying to get to?

Once you define what you really want to do, you should be able to see how to do it from the examples given. If you truly can't, see my Services and Rates pages.







Thu Apr 1 12:04:07 2010: 8332   anonymous

gravatar


what about:

CURRENT_DATE=$(TZ=0 date +"%Y-%m-%d")
echo $CURRENT_DATE

#CURRENT_DATE+1
NEW_DATE=$(TZ=-24 date +"%Y-%m-%d")
echo $NEW_DATE

#CURRENT_DATE+2
NEW_DATE=$(TZ=-48 date +"%Y-%m-%d")
echo $NEW_DATE

#CURRENT_DATE+3
NEW_DATE=$(TZ=-72 date +"%Y-%m-%d")
echo $NEW_DATE

dunno if it works backwards



Thu Apr 1 12:12:50 2010: 8333   TonyLawrence

gravatar


I suggest you read (link)

------------------------
Kerio Samepage


Have you tried Searching this site?

Unix/Linux/Mac OS X support by phone, email or on-site: Support Rates

This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more.

Contact us





If it looks like a duck, and quacks like a duck, we have at least to consider the possibility that we have a small aquatic bird of the family Anatidae on our hands. (Douglas Adams)





This post tagged: