Date

There is a shell script which runs on every friday and it generates a file in
yyyymmdd format for eg : 20111202.
When we manually run the script it generates files but it will take the system date .

My requirement is whatever the day it runs it should rename the file to the date of friday .

For eg : today is 20111207 - wednesday . if i am running the script today it will generate a file of date 20111207.But i want the date to be renamed to 20111202 ( friday ).

I am running the script in Hp unix .. Please suggest..

Check this function:

$ cat nextFriday.sh
getNextFridayDate()
{
	currentDayName=`date +"%A"`
	currentDate=`date +"%Y%m%d"`
	countDays=1
	while [ "${currentDayName}" != "Friday" ]
	do
		currentDayName=`date -d "${countDays} day" +"%A"`
		currentDate=`date -d "${countDays} day" +"%Y%m%d"`
		countDays=`expr ${countDays} + 1`
	done
	echo "${currentDate}"
}

nextFriDate=`getNextFridayDate`
echo "Next Friday is: [${nextFriDate}]"

$ ./nextFriday.sh
Next Friday is: [20111209]

I hope it helps. =o)

1 Like

Try a cron every Friday at 00:01 to create a file containing the date in the format you want. Then read that file into your script. To set the process in motion create the first file manually.

Btw. felipe.vinturin method will not work on HP-UX because it needs GNU date. It also calculates next Friday, not most recent Friday.

1 Like

Thanks felipe , Thanks methyl .. Actually i need to add a part on existing script with the logic

felipe : Thanks for your suggetion Actually i need to get in previous fridays..Lets say if my script fails on scheduled time which is friday ,. i will be manually running it on monday after fixing the issue .. So it should generate the file of friday's date on which the script got failed ( previous friday)..

methyl : - Thanks for ur suggestion ..But Cant schedule any extra jobs ..

Please suggest if there are any suggestions ..

I have thought of a idea.. but not givng prefrence as it seems to be bit length .
taking 7 variables ..
day1=Mon , day2 =Tue...day 5 - fri,day7 =Sun

and when the file will generate . let say its 20111209..will extract the day from it .. then will put a condition which will check

if result day == fri . ten echo "" elif result day = mon then result day - 3 will give friday . ..
something like tht ..

Its just a logic i thought of not implemented yet ..

Please suggest .

Thanks

Surely when the script runs on Friday it could start off by creating a timestamp file ready for the rerun? There is a hole in this idea if the job does not run at all on the Friday.

The Shell arithmetic approach tends to be verbose but not that difficult to write. It needs to work even if the rerun takes place on awkward dates like Mar 3rd in a leap year or any Jan 3rd.

Check the code below:

tempFile="./fridayDate.tmp"
##########################################################
checkRetCode ()
{
	lastRetCode=$?
	if [ ${lastRetCode} -ne 0 ]
	then
		echo "Last command's return code is invalid: [${lastRetCode}]. Exitting 1."
		exit 1
	fi
}
##########################################################
setFridayInfo ()
{
	currentDayName=`date +"%A"`
	if [ "${currentDayName}" == "Friday" ]
	then
		lastFridayDate=`date +"%Y%m%d"`
		echo "Today is Friday! Writing to temp file: [${tempFile}][${tempFile}]"
		echo "${lastFridayDate}" > "${tempFile}"
		checkRetCode
	fi

	if [ -f "${tempFile}" ]
	then
		lastFridayDate=`egrep '[0-9]+' "${tempFile}"`
		lastFridayDate="${lastFridayDate:-NULL}"
		if [ ${#lastFridayDate} -ne 8 ]
		then
			echo "Temp file has an invalid date: [${#lastFridayDate}][${lastFridayDate}]. Exitting 1."
			exit 1
		fi
	else
		echo "Temp file does not exist: [${tempFile}] and weekday name is different from Friday. Exitting 1."
		exit 1
	fi
	return 0
}

##########################################################
setFridayInfo

echo "${lastFridayDate}"

When your job runs on Friday, it will write to the temp file Friday's date (YYYYMMDD), on the other days, it will look for the date in the temp file.

I hope it helps.

1 Like

Thanks Felipe . Just have some queires..as you mentioned that ur script will create a temp file on friday ..So my script will be reading frm tht temp file..rite ??