Operating System - HP-UX
1833788 Members
2800 Online
110063 Solutions
New Discussion

File Name based on yesterday's date

 
SOLVED
Go to solution
Robert Comber
Advisor

File Name based on yesterday's date

Hello Gang,

I am trying to use A. Clay Stephenson's caljd.sh script to generate a filename based upon yesterday's date unless today is Monday. I want Monday to use the previous Friday. These files and directories will be used to store backup information.

For example, for Nov. 11, 2002 (a Monday) I want the name to
be 2001/Nov/09 (previous Friday) but for Nov. 12, 2002 (a Tuesday) I want the name to be 2002/Nov/11 (Monday).

Does anybody know how to get caljd.sh to do this? A.Clay, are you out there?

TIA, Bob
10 REPLIES 10
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: File Name based on yesterday's date

This is is actually quite easy bit make sure that you have Vrsn 2.1 of caljd.sh or caljd.pl.

FNAME=$(caljd.sh -y -S "/" -o $(caljd.sh -p 1 -x 0 -x 6))

the -p 1 says previous day; -x 0 skip Sun.; -x 6 skip Sat.

If I didn't make any typo's that should fix you.
If it ain't broke, I can fix that.
A. Clay Stephenson
Acclaimed Contributor

Re: File Name based on yesterday's date

By the way, if you also want to skip holidays (and your /etc/acct/holidays file is up to date), add -h to the second caljd.sh call and you are in business. Invoke as caljd.sh -u for full usage and examples.
If it ain't broke, I can fix that.
Robert Comber
Advisor

Re: File Name based on yesterday's date

Thanks Clay. You also made me realize that I do want to skip holidays as well. Your solution worked great!!

Bob
Dan Bolton
Frequent Advisor

Re: File Name based on yesterday's date

Where can I find this script (caljd.sh)? It looks like it may be the perfect solution to a request of one of my users.
...skid in sideways, chocolate in one hand, martini in the other, totally worn out and screaming, "WOO HOO what a ride!"
A. Clay Stephenson
Acclaimed Contributor

Re: File Name based on yesterday's date

Hi Dan:

If you use that little button called 'Search' and search for "caljd.sh", you can find it. To make it easy, I'll attach the latest version.

If it ain't broke, I can fix that.
A. Clay Stephenson
Acclaimed Contributor

Re: File Name based on yesterday's date

And here's the Perl equivalent. Invoke as caljd.pl (or caljd.sh) -u for full usage and many examples.

Regards, Clay
If it ain't broke, I can fix that.
Dan Bolton
Frequent Advisor

Re: File Name based on yesterday's date

Thanks for the quick response. BTW, I did try a search in the ITRC forums before I posted and received no hits. (I must have had a trailing space or something, 'cuz it works now!)

Thanks Again,
Dan
...skid in sideways, chocolate in one hand, martini in the other, totally worn out and screaming, "WOO HOO what a ride!"
Bruce Rhind
New Member

Re: File Name based on yesterday's date

Here's a quick and dirty...

o Add a cron job to touch a file every day at 23:59. For example:

59 23 * * 1-5 date +%Y/%b/%d > /tmp/yesterday

So on Tuesday, this file will contain Monday's date, on Wed. it'll contain Tuesday's date, and on Sat., Sun, and Monday, it'll have Friday's date.

o In your script, cat the /tmp/yesterday file to build your filename.

o Not perfect, but it works.

Maybe this'll help?
Beer is food.
Robert Comber
Advisor

Re: File Name based on yesterday's date

Thanks Bruce but I'll stick with A. Clay's script. I think it is now the de facto standard for date problems on the forums.

Clay,

Since you mentioned holidays, I don't want my cron backup scripts to run if it is a holiday. (Nobody will be here to change tapes.) In my cron script, is there an easy way to know if the current date is a holiday? My /etc/acct/holidays file is now current. I know I have to use -h, I just can't seem to get it quite right.

TIA, Bob

A. Clay Stephenson
Acclaimed Contributor

Re: File Name based on yesterday's date

Hi Bob:

You guys are going to make me write a tutorial on caljd.sh. You are going to kick yourself because it is so simple.

if [[ $(caljd.sh) -ne $(caljd.sh -h) ]]
then
echo "Today is a holiday."
exit 0
else
echo "Today ain't no holiday. Do your thing."
fi

All that happens is that if today is a holiday then the -h actually returns the next Julian Day but returns the same Julian Day otherwise.

If it ain't broke, I can fix that.