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 also.
Publish your articles, comments, book reviews or opinions here!
© August 1999 A.P. Lawrence. All rights reservedEnter your email address for automatic notification of new posts here
(be sure to whitelist 'feedburner.com' if you use spam filtering)
| Views for this page | ||||
|---|---|---|---|---|
| Today | This Week | This Month | This Year | Overall |
| 10 | 68 | 315 | 3,418 | 21,360 |
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. We appreciate comments and article submissions.

UnixYesterday :
---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: Subject: TonyLawrence
date --date='1 day ago'
is easier..
Thu May 5 13:09:47 2005: Subject: Many other simpler ways... 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: Subject: 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: Subject: Thanks for the function 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: Subject: anonymous
Thank you - it works!
Tue Mar 21 11:43:39 2006: Subject: TonyLawrence
See http://aplawrence.com/Linux/teach_fishing.html also.
There are dozens of ways to skin this cat.
Wed Oct 4 04:27:49 2006: Subject: Simple anonymous
yest
(sourceforge.net/projects/yest)
Lone-Tar Backup and Disaster Recovery
for Linux and Unix
Add your comments