3

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

3 Answers 3

4

I would do:

perl -MPOSIX -le '
  @t=localtime;
  print strftime "%Y/%m/%d", 
    localtime time - 86400*(($t[6]-1+7-$ARGV[0])%7+1)' 4

(where 4 above is the day of the week, 0 for Sunday, 4 for Thursday)

perl is generally the safest option for portable date manipulation.

2

Shellscript + date isn't really the most suitable tool here. The perl answer given already is good, though I prefer python's explicitness:

import datetime, sys

today = datetime.date.today()
wd = today.weekday() # Mon == 0, Sun == 6
wd_wanted = int(sys.argv[1])

date_wanted = today - datetime.timedelta((wd-wd_wanted)%7 or 7)

The or 7 bit solves the last monday when today is monday problem.

0

Since the posted questions ask for a ksh or bash answer for plain vanilla Solaris 'X' I suppose I'm not within guidelines but if you can install Tcl 8.5 you'd have access to the powerful clock command for date/time arithmetic:

Invoking tclsh ...

% set delta 7                                
7
% clock format [clock scan "now - $delta days"]
Fri Jan 11 00:00:00 EST 2013
2
  • Don't worry, none of the other answers so far uses pure shell + Solaris date anyways.
    – jw013
    Commented Jan 18, 2013 at 16:51
  • @jw013, the OP's question also used expr and echo so we can assume we can use whatever command is available in a vanilla Solaris 10 installation. Commented Jan 18, 2013 at 20:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .