Wednesday, October 15, 2008

DATE manipulation in UNIX CAL function

DATE manipulation in UNIX CAL function
At times we need to calculate specific date for running a particular job or program. For example running or creating a flag every Tuesday of the week. For this we need to pick the date through cal function in unix , example is as follows.
TUE1=`(cal $gvar2 | tail +3 | cut -c4,5 | sed '/^ *$/d' | sed -n '1p')`
TUE2=`(cal $gvar2 | tail +3 | cut -c4,5 | sed '/^ *$/d' | sed -n '2p')`
TUE3=`(cal $gvar2 | tail +3 | cut -c4,5 | sed '/^ *$/d' | sed -n '3p')`
TUE4=`(cal $gvar2 | tail +3 | cut -c4,5 | sed '/^ *$/d' | sed -n '4p')`
TUE5=`(cal $gvar2 | tail +3 | cut -c4,5 | sed '/^ *$/d' | sed -n '5p')`

if [ "$TUE1" -eq "" ]
then
TUE1=0
fi

if [ "$TUE5" -eq "" ]
then
TUE5=0
fi

if [ -f ${tokendir}/OHS_EMP_WEEK_RUN.FLG ]; then
exit 0;
else
if [ $C_Date -eq $TUE1 ]; then
touch ${tokendir}/OHS_EMP_WEEK_RUN.FLG
else
if [ $C_Date -eq $TUE2 ]; then
touch ${tokendir}/OHS_EMP_WEEK_RUN.FLG
else
if [ $C_Date -eq $TUE3 ]; then
touch ${tokendir}/OHS_EMP_WEEK_RUN.FLG
else
if [ $C_Date -eq $TUE4 ]; then
touch ${tokendir}/OHS_EMP_WEEK_RUN.FLG
else
if [ $C_Date -eq $TUE5 ]; then
touch ${tokendir}/OHS_EMP_WEEK_RUN.FLG
else
touch ${tokendir}/OHS_EMP_WEEK_NOT_RUN.FLG
fi
fi
fi
fi
fi
fi


In the above code we find every TUE for creating FLAG. $gvar2 will be month and year to be passed.

No comments: