I'm trying desperatly to find a bash or ksh routine that allows me to find for example the previous Monday,Tuesday,Wednesday,... preceding today's date. Additonal it has to work on plain vanilla Solaris X and I don't have the GNU date available.
eg: Today = Thursday 2013/01/17 ; Let's say I want to find the last Monday. It has to return: 2013/01/14
I've managed to find a script on the net that does the job perfectly for all days except in this specific case: eg: Today = Thursday 2013/01/17 ; I want to find the last Thursday which should give as result: 2013/01/10 ; but instead I get todays date again.
The script used was this:
#!/bin/ksh #Get the nbr of the current weekday (1-7) DATEWEEK=`date +"%u"` #Which previous weekday will we need (1-7) WEEKDAY=$1 # Main part #Get current date DAY=`date +"%d"` MONTH=`date +"%m"` YEAR=`date +"%Y"` #Loop trough the dates in the past COUNTER=0 if [[ $DATEWEEK -eq $WEEKDAY ]] ; then # I need to do something special for the cases when I want to find the date of the same day last week DAYS_BACK=168 DAY=`TZ=CST+$DAYS_BACK date +%d` echo "DAY (eq) = $DAY" else while [[ $DATEWEEK -ne $WEEKDAY ]] ; do COUNTER=`expr $COUNTER + 1` echo "Counter is: $COUNTER" DAYS_BACK=`expr $COUNTER \* 24` echo "DAYS BACK is: $DAYS_BACK" DAY=`TZ=CST+$DAYS_BACK date +%d` echo "DAY is: $DAY" if [[ "$DAY" -eq 0 ]] ; then MONTH=`expr "$MONTH" - 1` if [[ "$MONTH" -eq 0 ]] ; then MONTH=12 YEAR=`expr "$YEAR" - 1` fi fi DATEWEEK=`expr $DATEWEEK - 1` if [[ $DATEWEEK -eq 0 ]]; then DATEWEEK=7 fi done fi echo $DAY/$MONTH/$YEAR