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!
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.
Many of the products and books I review are things I purchased for my own use. Some were given to me specifically for the purpose of reviewing them. I resell or can earn commissions from the sale of some of these items. Links within these pages may be affiliate links that pay me for referring you to them. That's mostly insignificant amounts of money; whenever it is not I have made my relationship plain. I also may own stock in companies mentioned here. If you have any question, please do feel free to contact me.
Specific links that take you to pages that allow you to purchase the item I reviewed are very likely to pay me a commission. Many of the books I review were given to me by the publishers specifically for the purpose of writing a review. These gifts and referral fees do not affect my opinions; I often give bad reviews anyway.
We use Google third-party advertising companies to serve ads when you visit our website. These companies may use information (not including your name, address, email address, or telephone number) about your visits to this and other websites in order to provide advertisements about goods and services of interest to you. If you would like more information about this practice and to know your choices about not having this information used by these companies, click here.
Click here to add your comments
---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)
Fri Oct 24 05:20:06 2008: Subject: anonymous
Thanks I needed this....
Wed Dec 10 19:17:20 2008: Subject: 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: Subject: TonyLawrence
Already mentioned in the comments above yours:
if [ $MONTH -le 10 ]
then
MONTH='0'$MONTH
fi
Wed Feb 4 08:44:12 2009: Subject: anonymous
how to minus 1 month instead of minus 1 day??
Wed Feb 4 12:26:23 2009: Subject: TonyLawrence
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.
Don't miss responses! Subscribe to Comments by RSS or by Email
Click here to add your comments
If you want a picture to show with your comment, go get a Gravatar