Solaris: find the day of last Monday,Tuesday,...Sunday by means of shell script

5,973

Solution 1

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.

Solution 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.

Share:
5,973

Related videos on Youtube

David Thijs
Author by

David Thijs

Updated on September 18, 2022

Comments

  • David Thijs
    David Thijs over 1 year

    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
    
  • jw013
    jw013 over 11 years
    Don't worry, none of the other answers so far uses pure shell + Solaris date anyways.
  • Stéphane Chazelas
    Stéphane Chazelas over 11 years
    @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.