Operating System - HP-UX
1825805 Members
2217 Online
109687 Solutions
New Discussion

Re: File Name based on Prior date

 
SOLVED
Go to solution
James Kirk_1
Occasional Contributor

File Name based on Prior date

Hello,

I need to create a filename based upon the previous day's date but here is where it gets hard. If today is Monday, I want to use the previous Friday. If today is Tuesday through Friday, I want to use the previous day.

I would like the filename to look like Dyymmdd.BKUP.

The date command doesn't seem to do what I want. Any ideas?

Thanks,
Jim
8 REPLIES 8
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: File Name based on Prior date

Easy,

FNAME="D$(caljd.sh -C -y -s $(caljd.sh -p 1 -x 0 -x 6)).BKUP"


Search the Forums for caljd.sh but make sure that you find Version 2.22 (or later). There is also a Perl version, caljd.pl that works exactly the same.

If it ain't broke, I can fix that.
Sridhar Bhaskarla
Honored Contributor

Re: File Name based on Prior date

Hi Jim,

A little bit of perl (only to get the time since epoch - you should have in your /usr/contrib/bin) and adb should get you through it. You will get full date as YDATE and you can chop to whatever you want like MYDATE and use it as an extension.

-Sri

#!/usr/bin/ksh
NOW=$(/usr/contrib/bin/perl -e "printf("%d\n",time())")
WEEK=$(date +%a)

if [ "$WEEK" != "Mon" ]
then
COUNT=1
else
COUNT=3
fi

(( TIME = $NOW - ( $COUNT * 86400 ) ))

YDATE=$(echo "0d${TIME}=Y" |adb)

echo Full Date is $YDATE

MYDATE=$(echo $YDATE|awk '{print $2"-"$3"-"$1}')
echo $MYDATE
You may be disappointed if you fail, but you are doomed if you don't try
Chris Vail
Honored Contributor

Re: File Name based on Prior date

Yeah, you can do this, but its messy.
TODAY=`date -u +%a`
case $TODAY in
MON) YSDAY=FRI;;
TUE) YSDAY=MON;;
WED YSDAY=TUE;;
THU YSDAY=WED;;
FRI YSDAY=THU;;
esac

filename=$YSDAYmmddyy

This of course only fixes the day of the week, not the date itself.
Patrick Wallek
Honored Contributor

Re: File Name based on Prior date

Here's my version using A. Clay's caljd.sh. It's not nearly as pretty, but it does work.

The first version gives you a file name like D20040202.BKUP.

#!/usr/bin/sh

DOW=$(caljd.sh -w)

if [ "$DOW" = 1 ]
then
DATE=$(caljd.sh -y -s $(caljd.sh -p 3))
else
if [ "$DOW" != 6 -a "$DOW" != 0 ]
then
DATE=$(caljd.sh -y -s $(caljd.sh -p 1))
fi
fi
FILE="D${DATE}.BKUP"
echo $FILE


This version cuts the leading '20' off of the year to give you a file name like D040202.BKUP:

#!/usr/bin/sh

DOW=$(caljd.sh -w)

if [ "$DOW" = 1 ]
then
DATE=$(caljd.sh -y -s $(caljd.sh -p 3))
else
if [ "$DOW" != 6 -a "$DOW" != 0 ]
then
DATE=$(caljd.sh -y -s $(caljd.sh -p 1))
fi
fi
DATE=$(echo $DATE | cut -c 3-8)
FILE="D${DATE}.BKUP"
echo $FILE
RAC_1
Honored Contributor

Re: File Name based on Prior date

YESTERDAY=$(TZ=$(date +%Z)+24; date '+%b %e') gives yesterday's date.

There is no substitute to HARDWORK
Hein van den Heuvel
Honored Contributor

Re: File Name based on Prior date

What did you mean with Dyymmdd?
Does that D stand for?
Just a "D" or "Day of the week"?
If it is a "D" check out:

while (1){
$t -= 86400;
($x,$x,$x,$d,$m,$y,$w,$x,$x) = localtime($t);
last if ($w % 6);
}
printf ("D%02d%02d%02d.BKUP\n",$y%100,++$m,$d;


If it means "Day of the week" try this.
(remove testing code when satisfied)

$x = shift @ARGV;
$t = time + ($x * 86400);
while (1){
$t -= 86400;
($x,$x,$x,$d,$m,$y,$w,$x,$x) = localtime($t);
last if ($w % 6);
}
printf ("%s%02d%02d%02d\n",("Mon","Tue","Wed","Thu","Fri")[--$w],$y%100,++$m,$d;




T G Manikandan
Honored Contributor

Re: File Name based on Prior date

Clay -you can rename this script as

caljd.sh-->clay.sh

Hein van den Heuvel
Honored Contributor

Re: File Name based on Prior date

Ooops,

I forgot to exlicitly indicate that my examples are PERL scripts. Not that there can be much doubt, but still.

[Arrghh... I wish I could just edit that reply]

Hein.