How do I calculate the previous business day in ksh shell script?
13,743
Here is a solution that doesn't use Perl. It works both with ksh
and sh
.
#!/bin/ksh
diff=-1
[ `date +%u` == 1 ] && diff=-3
seconds=$((`date +%s` + $diff * 24 * 3600))
format=+%Y-%m-%d
if date --help 2>/dev/null | grep -q -- -d ; then
# GNU date (e.g., Linux)
date -d "1970-01-01 00:00 UTC + $seconds seconds" $format
else
# For BSD date (e.g., Mac OS X)
date -r $seconds $format
fi
Author by
Admin
Updated on August 02, 2022Comments
-
Admin almost 2 years
What is the most elegant way to calculate the previous business day in shell ksh script ?
What I got until now is :
#!/bin/ksh set -x DAY_DIFF=1 case `date '+%a'` in "Sun") DAY_DIFF=2 ;; "Mon") DAY_DIFF=3 ;; esac PREV_DT=`perl -e '($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time()-${DAY_DIFF}*24*60*60);printf "%4d%02d%02d",$year+1900,$mon+1,$mday;'` echo $PREV_DT
How do I make the ${DAY_DIFF} variable to be transmitted as value and not as string ?
-
jwg almost 9 years'Business day' is a standard term which never includes Sat and Sun.