Inclusive next weekday is built in, just use -d"Thursday"
, (no prefix), and date
will return today
if today is Thursday, or next Thursday if not.
But date
has no inclusive last, so here's an inclusive last weekday kludge function:
ilast() { a=$(date -d'today' "+%A") ; b=$(date -d"$1" "+%A") ; \
[ $a != $b ] && echo -n "last " ; echo $b ; }
Test, (which also shows ilast
's usage):
for d in Sun. Mon. Tue. Wed. Thu. Fri. Sat. ; do \
echo -n $(ilast $d)" " ; \
date -d"$(ilast $d)" "+%Y%m%d" ; \
done | column -t
Output:
last Sunday 20160626
last Monday 20160627
last Tuesday 20160628
last Wednesday 20160629
Thursday 20160630
last Friday 20160624
last Saturday 20160625
The function sets $a to today
's weekday, and $b to $1's weekday, which would be a different weekday, unless $1 is today's weekday. So if the weekdays $a and $b are not equal, (six days out of seven they'd differ), we prepend "last " to the weekday's long name. If the weekdays $a and $b are equal, don't print "last ", since, (one day out of seven), the default inclusive next already does the desired thing.