How to derive the last Sunday's date when Current date value is passed

Hi All,

I have a requirement in my project where a batch runs on any day of a week. The input files will land to the server every Sunday. I need to read these files based on the current BUS_DAY (which falls any day of the week).

For e.g : if the BUS_DAY is 20120308, we need to derive the filedate as 20120304. I am trying the same as below,

export DAYS_AGO=`date +%w`
echo $DAYS_AGO
export SUB_DATE=`date -d "-$DAYS_AGO day" "+%Y%m%d"`
echo "P_SUB_DATE=$SUB_DATE" >>$COMMON_TMP/CPMGDynamicDateFile.txt

Can you pls let me know whether this will work for dates like 20120302(first days of the month). Any better approach will be much appreciated.

Thanks
Freddie

Seems to work.

$ date -d "20120308 - 4 days" +%Y%m%d
20120304

$

It's important to remember that this is a feature of GNU date only, and perhaps only newer versions of GNU date. Systems other than Linux are very unlikely to have it.

Thanks for your reply Corona.

I am using LINUX & i hope this command will work fine without any issues.

My only concern is whether this command will derive the correct value for the dates like 20120302. In this case, the value derived should be 20120226. Since i dont have privileges to change the system date, I am unable to test it.

Can you Pls help me/

Thanks
Freddie

You dont need to change the system date just use:

$ date -d "20120302 - 5 days" +%Y%m%d
20120226

The above just prints the date 5 days prior to Mar 02 2012, it dosn't change the system date.