How to get the last 'N' business days in a Unix shell script using a `for` loop?
5,952
Solution 1
If you want just 5 working days before today try:
for d in Mon Tue Wed Thu Fri
do
date +%Y%m%d -d "last $d"
done | sort
For any N-days before today:
N=10
for i in $(seq $(($N + $N / 5 * 2)) -1 1)
do
[ `date --date="-$i day" +%u` -le 5 ] &&
date -d "-$i day" +"File date is : %Y%m%d"
done
Solution 2
I understand you actually want N days listed rather than the hardcoded five you have shown in your question. Here is a bash
solution that will address this requirement:
n=5
today=$(date +'%Y-%m-%d')
for ((d=1; n>0; d++))
do
# Adjust this next line to get the date format you require, e.g. +'%Y%m%d'
date=$(date --date "$today -$d day")
nday=$(date --date "$today -$d day" +'%w')
if [[ nday > 0 && nday < 6 ]]
then
# Adjust this next line to output whatever you really need
echo "n=$n, d=$d, nday=$nday, date=$date: WEEKDAY"
((n--))
fi
done
Output for n=5
, run on 5th November 2015
n=5, d=1, nday=3, date=Wed, 4 Nov 2015 00:00:00: WEEKDAY
n=4, d=2, nday=2, date=Tue, 3 Nov 2015 00:00:00: WEEKDAY
n=3, d=3, nday=1, date=Mon, 2 Nov 2015 00:00:00: WEEKDAY
n=2, d=6, nday=5, date=Fri, 30 Oct 2015 00:00:00: WEEKDAY
n=1, d=7, nday=4, date=Thu, 29 Oct 2015 00:00:00: WEEKDAY
Related videos on Youtube
Author by
srikanth
Updated on September 18, 2022Comments
-
srikanth over 1 year
The following script:
N=5 BUSINESS_DATE=`date -d "-2 day" +"%Y%m%d"` for (( c=0; c<N ; c++ )) do WEEKDAY=`date --date="$BUSINESS_DATE -$c day" +%w` if [ $WEEKDAY == "0" ] then FILE_DT_TMP=`date --date="$BUSINESS_DATE -$c day-2day" +%Y%m%d`; elif [ $WEEKDAY == "6" ] then FILE_DT_TMP=`date --date="$BUSINESS_DATE -$c day -2day" +%Y%m%d`; elif [ $WEEKDAY == "5" ] then FILE_DT_TMP=`date --date="$BUSINESS_DATE -$c day -2day" +%Y%m%d`; elif [ $WEEKDAY == "4" ] then FILE_DT_TMP=`date --date="$BUSINESS_DATE -$c day -2day" +%Y%m%d`; elif [ $WEEKDAY == "3" ] then FILE_DT_TMP=`date --date="$BUSINESS_DATE -$c day -2day" +%Y%m%d`; elif [ $WEEKDAY == "2" ] then FILE_DT_TMP=`date --date="$BUSINESS_DATE -$c day -2day" +%Y%m%d`; elif [ $WEEKDAY == "1" ] then FILE_DT_TMP=`date --date="$BUSINESS_DATE -$c day -2day" +%Y%m%d`; else FILE_DT_TMP=`date --date="$BUSINESS_DATE -$c day" +%Y%m%d` fi export FILE_DT=$FILE_DT_TMP echo "File date is :$FILE_DT" done
is not giving proper output. Can anyone advise me – what’s wrong, and how do I fix it?
-
G-Man Says 'Reinstate Monica' over 8 years(0) Format your code so it’s readable. (garethTheRed and I did that for you, including correcting the spelling of “business”.) (1) Explain what you want it to do, in the body of the question. (2) Say what it is doing. (3) Describe what you have done to solve the problem yourself.
-
Costas over 8 years
for d in Mon Tue Wed Thu Fri ; do date +%Y%m%d -d "last $d" ; done | sort
is not acceptable? -
Alessio over 8 yearsthis doesn't answer your question buy see also the
cal
command for a nicely formatted printout of the current month (or, with options, several months, or year).
-
-
RobertL over 8 yearsOK. Thanks. As you can see your example did not work before I made the above changes. It's a little confusing when the example doesn't work.
-
srikanth over 8 yearsTry to get required output na
-
srikanth over 8 yearsThanks for the formating. i was in a hurrey so could nt do it that way.. Stil i did not get the required output.I want last "n" business days.
-
Alessio over 8 years@srikanth was the problem caused by the missing space between
day
and-2day
for the0
case that i pointed out? edit your question to show an example of the ouput you are getting and an example of the output you expect to get. -
srikanth over 8 yearsSorry I want "N" business days not just 5.
-
roaima over 8 years@srikanth please edit your question to include this new information. Otherwise we aren't going to know, because the title of your post clearly states FIVE days, and your example code also uses a hard-coded 5.
-
Costas over 8 years@srikanth See updated
-
Jeff Schaller almost 5 yearsThis is the last 5 days, while the question asks for the last five business days. Notice their logic that checks for the day of the week.