1833231 Members
2653 Online
110051 Solutions
New Discussion

Re: Script help - Dates

 
SOLVED
Go to solution
Greg Stark_1
Frequent Advisor

Script help - Dates

I have a faxing app that I need to write a simple script to purge the outbound fax entries. The command used is as follows:

faxdbdel -OUTBOUND -to YYMMDD

The user wants to keep 2 weeks worth in the db. I figured I would write a cron script which is called daily to accomplish this, but I'm having some problems trying to load the command with the correct day (DD) of 2 weeks ago. Any ideas?

thanks,
Greg
12 REPLIES 12
Sachin Patel
Honored Contributor

Re: Script help - Dates

Hi Greg,
To get the day of month is
date +%d day of month
date +%a for week day etc..

If you don't mind post your script or part of script which has problem.

Sachin
Is photography a hobby or another way to spend $
Michael Tully
Honored Contributor

Re: Script help - Dates

Hi,

I'm certainly not going to take credit for this, but Mr Stephenson's 'caljd.sh' script will do this job. I'm sure you find it easily by searching, or it may even be posted by Clay himself.

Cheers
~Michael~
Anyone for a Mutiny ?
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: Script help - Dates

Hi Greg: This rather cryptic shell line will do the trick:

DAY=$(echo $(caljd.sh -y -s $(caljd.sh -p 14)) | cut -c 3-)
echo "DAY=${DAY}"

The inner caljd.sh returns the Julian Day 14 days ago and the outer caljd.sh says format in YYYY MM DD format (-e) and reove the spaces (-s) to yield YYYYMMDD. The cut then says returns characters 3 to the end of string or YYMMDD - just what you asked for.

See the attached caljd.sh. Caljd.sh -u will give full usage.

If it ain't broke, I can fix that.
Craig Rants
Honored Contributor

Re: Script help - Dates

I agree, basically Clay's script is the defacto standard for date calculations in this forum.

GL,
C
"In theory, there is no difference between theory and practice. But, in practice, there is. " Jan L.A. van de Snepscheut
Sachin Patel
Honored Contributor

Re: Script help - Dates

Woo nice script Mr. Clay.

Thanks
Is photography a hobby or another way to spend $
Greg Stark_1
Frequent Advisor

Re: Script help - Dates

I agree, I wish I had it for some previous scripts.

Thanks,again.
Greg Stark_1
Frequent Advisor

Re: Script help - Dates

Ok, one more question. With the help of caljd.sh I can return the the month of the year from 14 days ago in a 2-digit form. Besides using a long case or if/elsif statement, is there an easy way to convert a 2-digit month to the corresponding 3 letter string?

For instance:
04=Apr

thanks,
Greg
A. Clay Stephenson
Acclaimed Contributor

Re: Script help - Dates

That's fairly easy too; we just take advantage of the shell's arrays:

#!/usr/bin/sh

MO[01]="Jan"
MO[02]="Feb"
MO[03]="Mar"
MO[04]="Apr"
MO[05]="May"
MO[06]="Jun"
MO[07]="Jul"
MO[08]="Aug"
MO[09]="Sep"
MO[10]="Oct"
MO[11]="Nov"
MO[12]="Dec"

DAY=$(echo $(caljd.sh -y -s $(caljd.sh -p 14)) | cut -c 3-)
echo "DAY=${DAY}"
MONTH=${MO[$(caljd.sh -M $(caljd.sh -p 14))]}
echo "Month=${MONTH}"


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

Re: Script help - Dates

Hi again Greg:

Yet another way to convert 04 to "Apr" although I like the previous method better; it's less obscure.

#!/usr/bin/sh


SMONTH="???JanFebMarAprMayJunJulAugSepOctNovDec"

DAY=$(echo $(caljd.sh -y -s $(caljd.sh -p 14)) | cut -c 3-)
echo "DAY=${DAY}"

NMONTH=$(caljd.sh -M $(caljd.sh -p 14))
OFFSET=$(((${NMONTH} * 3) + 1))
STOP=$((${OFFSET} + 2))
MONTH=$(echo ${SMONTH} | cut -c ${OFFSET}-${STOP})
echo "MONTH=${MONTH}"
If it ain't broke, I can fix that.
Greg Stark_1
Frequent Advisor

Re: Script help - Dates

I think I follow them both, but why all the ?'s in the SMONTH string?

Thank again,
Greg
A. Clay Stephenson
Acclaimed Contributor

Re: Script help - Dates

Hi Greg:

Without the dummy values for month 0 the offset calculations become offset = ((month - 1) * 3) + 1 - even more messy.
If it ain't broke, I can fix that.
A. Clay Stephenson
Acclaimed Contributor

Re: Script help - Dates

Okay Greg last time:

Your last question did make me realize that there was a better way still to do the offset into the month string. Use two '?'s and then it is not necessary to subtract 1 so that offset = (month * 3).

#!/usr/bin/sh


SMONTH="??JanFebMarAprMayJunJulAugSepOctNovDec"

DAY=$(echo $(caljd.sh -y -s $(caljd.sh -p 14)) | cut -c 3-)
echo "DAY=${DAY}"

NMONTH=$(caljd.sh -M $(caljd.sh -p 14))
OFFSET=$((${NMONTH} * 3))
STOP=$((${OFFSET} + 2))
MONTH=$(echo ${SMONTH} | cut -c ${OFFSET}-${STOP})
echo "MONTH=${MONTH}"
If it ain't broke, I can fix that.