Help to get date

Hi,

I need some help... i mean can someone give some idea on how to implement what i want to do...

I have script called Mail.ksh which calls another script called read.ksh. read.ksh appends data to time.out file...This Mail.ksh runs everyday say 9:00AM on cron... We have like 50 Jobs on cron and read.ksh collects data form these 50 jobs and appends the data to time.out

time.out file looks like this

 
.
.
.
.
20090123,00:02:33,00:00:35,8134,00:01:42,00:01:32,02:02:08,321055, 
20090124,00:02:33,00:00:35,9345,00:01:42,00:01:32,02:02:08,321055, 
20090125,00:02:33,00:00:35,8783,00:01:42,00:01:32,02:02:08,321055, 
20090126,00:02:33,00:00:35,44554,00:01:42,00:01:32,02:02:08,321055, 

The mail.ksh calculates the current mantaince date using the below code

 
julian2date() # julianday
{
typeset -i day month year tmpday
julianday=$1
((tmpday = julianday - 1721119))
((centuries = (4 * tmpday - 1) / 146097))
((tmpday += centuries - centuries/4))
((year = (4 * tmpday - 1) / 1461))
((tmpday -= (1461 * year) / 4))
((month = (10 * tmpday - 5) / 306))
((day = tmpday - (306 * month + 5) / 10))
((month += 2))
((year += month/12))
((month = month % 12 + 1))
MONTH_LEN=$(echo ${month} | awk '{print length($1)}')
if [[ ${MONTH_LEN} = 1 ]]
then
 format_month="0${month}"
else
 format_month=${month}
fi
DAY_LEN=$(echo ${day} | awk '{print length($1)}')
if [[ ${DAY_LEN} = 1 ]]
then
 format_day="0${day}"
else
 format_day=${day}
fi
print $year$format_month$format_day
}
########################################################
# Get the Julian equivalent of the current date, subtract 1 day, convert back to a date
typeset -i LSD_DAY LSD_MONTH LSD_YEAR LSD_JUL CMD_JUL CUR_MAINT_DATE
YYYYMMDD=`date +%Y%m%d`
LSD_DAY=$(echo ${YYYYMMDD} | awk '{print substr($1,7,2)}')
LSD_MONTH=$(echo ${YYYYMMDD} | awk '{print substr($1,5,2)}')
LSD_YEAR=$(echo ${YYYYMMDD} | awk '{print substr($1,1,4)}')
LSD_JUL=$(date2julian ${LSD_DAY} ${LSD_MONTH} ${LSD_YEAR})
(( CMD_JUL = LSD_JUL - 1 ))
CUR_MAINT_DATE=$(julian2date ${CMD_JUL})

Usually the date will be previous daye ie if mail.ksh script is running at 9:00AM on 2009 01 27 then current mantaince date will be 2009 01 26
This is what the above code do.. The Mail.ksh takes the current maint date and pass it to read.ksh when will go a folder where the data of the files generated on current maint date are stored and collect the required data form them and appends the collected data to time.out

Now i want to change this... When ever mail.ksh runs it not only do the process for current maint date but also go back two dats and gets data...
like if its running on 27 then its also runs for data 26 and 25
get the data and place the data in the time.out such a way that it removes previous data and addes this data in the respective dates...
I want it to run 2 days back and remove data in time.out to respective date and place what so ever new data...

Please help me how to approch this problem...

Thanks a lot

-Bhagya

Bhagya,

Try to separate functionality (goals) from mechanism (steps to implement). Similarly, separate the function of performing maintenance from the step of finding the date. Pass the date into the function, and if you want to use another date, just call the function (or script) again with the different date.

Now, the code above is very complicated for doing what the date command already does. And out of curiosity, who needs Julian days? Okay, so you need them. Use the date command like this:

#Replace the lines above
# LSD_JUL=$(date2julian ${LSD_DAY} ${LSD_MONTH} ${LSD_YEAR})
#With...
LSD_JUL=$(date2julian $(date +"%Y %m %d"))
#and 
# CUR_MAINT_DATE=$(julian2date ${CMD_JUL})
#With...
CUR_MAINT_DATE=$(date +"%Y%m%d")

No need for all that other stuff.

mail.ksh is going to be kept in cron so i cant give the data from out side... i want something like a for loop...
where in code i will give some x=3 so the loop starts two days back calculates date from taking out 2 days form Julian date and calls read.ksh with 2 days back date ....and when it get back it should reamove 1 from x ie x =2 and calculate date and calls read.ksh and then x=1... i want it like this...


x=3
for (i=x; i<=x; x--) # i want the loop to go from 3 to 2 to 1 and exit when i=0
do
typeset -i LSD_DAY LSD_MONTH LSD_YEAR LSD_JUL CMD_JUL CUR_MAINT_DATE
YYYYMMDD=`date +%Y%m%d`
LSD_DAY=$(echo ${YYYYMMDD} | awk '{print substr($1,7,2)}')
LSD_MONTH=$(echo ${YYYYMMDD} | awk '{print substr($1,5,2)}')
LSD_YEAR=$(echo ${YYYYMMDD} | awk '{print substr($1,1,4)}')
LSD_JUL=$(date2julian ${LSD_DAY} ${LSD_MONTH} ${LSD_YEAR})
(( CMD_JUL = LSD_JUL - i ))
CUR_MAINT_DATE=$(julian2date ${CMD_JUL})

read.ksh ${CUR_MAINT_DATE} >> time.out


is this possible... if so please help me in putting it right order

There's no need for awk. Populate all three variables with a single call to date:

eval "$( date +" LSD_DAY=%d LSD_MONTH=%m LSD_YEAR=%Y" )"

There's a library of shell functions for date manipulation at The Dating Game.

Thanks... but organally it was written by someone... i bacially not a unix person... if i change in one ksh they will ask me to change on 65 more khs... i dont want to... let it be as it is till it does the work...

I want to know about the for loop i wrote before... please let me know will it work... if i write it like that.... say me any correction i need in the loop

CFAJ, you rock.

If you change 65 files to include one file, then future changes will require only one file to change. Also, 65 files, assuming each takes a minute to change, will be an hour of your time. How long will you waste on doing it the hard way?? :slight_smile:

You need:

for i in 3 2 1