Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to get date from 2 months ago

Status
Not open for further replies.

dickiebird

Programmer
Feb 14, 2002
758
GB
Hi Experts
I have a script which I edit each month,
which compresses and moves 2-month-old files
(sample file names are filea.APR.2002 )
eg

MON=APR
YR=2002
compress filea.${MON}.${YR}
mv filea.${MON}.${YR}.Z /archive_dir

I'd prefer not to edit the script each month, but get
the date changed appropriately.

(I can easily set $YR to previous year when current
month is Jan or Feb, just as I can set current month
short name to uppercase :

MON=`date +%b|awk '{printf("%s\n", toupper($0))}'`

But how d'you get $MON from 2 months ago as short
3 char uppercase name ??
Thanks in advance
;-) Dickie Bird
db@dickiebird.freeserve.co.uk
 
Hi dickiebird,

the following script cold be an idea (although it is dependant on when exactly you want to get the name of the month - at the beginning of each month, at the end, any time?)

So if you run it like

$ days_back.ksh 60
(60 days back)
the result is:
APR

#!/bin/ksh

[[ $# -ne 1 ]] && echo "usage: days_back.ksh days"

DAYS_OFF=$1

if [[ -n $DAYS_OFF ]]
then
((OFFSET = 24 * $DAYS_OFF))
TZ="$TZ+$OFFSET"
month_back=`date +%b`
# get the month name in upper case
typeset -u month_back
echo $month_back
fi

exit 0

I hope this is useful for you.

mrjazz [pc2]
 
Hi MrJazz
No joy unfortunately !
When I echo TZ on AIX 4.3.3 I get GMT0BST,M3.4.0,M10.5.0
When I include your lines in a script I get JUN - ie this month - so the TZ="$TZ+$OFFSET" ain't evaluated ?
Any thoughts ?
;-)
Dickie Bird
db@dickiebird.freeserve.co.uk
 
Hi Dickie Bird,

on Solaris (that's where I wrote the script) it works because $TZ=GMT (or whatever timezone you set).
Too bad that this doesn't work on AIX. :-(

Have you got a /etc/TIMEZONE file (on Solaris thes is a sym link to /etc/default/init)? I heard that you can change the TZ variable to a simple value (like GMT). Have you got a machine where you can test that? Unfortunately I don't have any access to a box on which AIX is running.

mrjazz
 
I'd have liked a neat solution - but I could always just do comparisons - ie If Jun, use Apr etc etc - Just isn't so neat.
Thanks for your efforts.
B-)
Dickie Bird
db@dickiebird.freeserve.co.uk
 
Dickie -- do you have Perl? Mike
________________________________________________________________

"Experience is the comb that Nature gives us, after we are bald."

Is that a haiku?
I never could get the hang
of writing those things.
 
Mike - I do have perl - but having never used it, or read ANYTHING about it - would it be wise for me to introduce such outlandishly new ideas to my head ??????
B-)
- Play hard - but play safe - Dickie Bird
db@dickiebird.freeserve.co.uk
 
What I came up with ( which works ) was :

typeset -i RUN_MONTH_NO=`date +%m`
typeset -i YEAR=`date +%Y`
set -A MONTH_NAMES xxx JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC
if [ ${RUN_MONTH_NO} -eq 1 -o ${RUN_MONTH_NO} -eq 2 ]
then
typeset -i RUN_MONTH_NO=${RUN_MONTH_NO}+10
typeset -i YEAR=${YEAR}-1
else
typeset -i RUN_MONTH_NO=${RUN_MONTH_NO}-2
fi
MONTH=${MONTH_NAMES[${RUN_MONTH_NO}]}

then I moved filea to filea-${MONTH}-${YEAR} and compressed it
;-)
Dickie Bird
db@dickiebird.freeserve.co.uk
 
you beat me to it Dickie :) Mike
________________________________________________________________

"Experience is the comb that Nature gives us, after we are bald."

Is that a haiku?
I never could get the hang
of writing those things.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top