Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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
share|improve this question
add comment

3 Answers 3

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.

share|improve this answer
add comment

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|improve this answer
add comment

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
share|improve this answer
    
Don't worry, none of the other answers so far uses pure shell + Solaris date anyways. –  jw013 Jan 18 '13 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. –  Stéphane Chazelas Jan 18 '13 at 20:09
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.