Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bash shell script for yesterdays date (last working day)

Tags:

date

bash

I am writing a bash script that needs to print the date of the last working day. So for example if the script is run on a Monday, it will print the date for last Friday.

I found that this prints yesterdays date:

date -d '1 day ago' +'%Y/%m/%d'

I also know that I can get the day of the week by using this statement

date +%w

I want to combine these two statements in order to have a little helper script that prints the required date. The logic goes something like this (note: its Pseudo code - I've never written a bash script)

DAY_OF_WEEK = `date +%w`
if (%DAY_OF_WEEK == 1)
   LOOK_BACK = 3
elif   
   LOOK_BACK = 1
fi

echo `date -d '%LOOK_BACK day ago' +'%Y/%m/%d'`

Can someone help by correcting the pseudo code above?

(I am running on Ubuntu 10.0.4)

like image 662
oompahloompah Avatar asked Mar 21 '11 21:03

oompahloompah


1 Answers

You were so close:

day_or_week=`date +%w`
if [ $day_or_week == 1 ] ; then
  look_back=3
else
  look_back=1
fi

date -d "$look_back day ago" +'%Y/%m/%d'
like image 130
kevin cline Avatar answered Oct 16 '22 00:10

kevin cline