#!/bin/bash
Usage () {
echo "Usage"
echo "./test.sh [-n <day+n>] [-d <first_date>] [-h <help>]"
exit -1
}
while getopts :n:d:h arg
do
case $arg in
n)
n=$OPTARG
echo "day+n: $OPTARG"
;;
d)
month_firstday=$OPTARG
echo "Specific month firstday: $OPTARG"
;;
h)
echo "-n: in day+n execute"
echo "-first_date: first date of the month, default is this month"
Usage
exit 1
;;
?)
echo "Invaild option: -$OPTARG"
exit 2
;;
esac
done
if [ ! $month_firstday ];then
month_firstday=$(date -d now +%Y-%m-01)
echo "This month firstday: $month_firstday"
fi
weekday=$(date -d $month_firstday +%w)
result_list=()
for str in $n
do
# the first day of the current month is Mon,Tue,Wed,Thu,Fri
if [ $weekday -le 5 ]&&[ $weekday -gt 0 ];then
firstweek_workday=$((6-$weekday))
left_workday=$(($str-$firstweek_workday))
# remaining working day>=0
if [ $left_workday -ge 0 ];then
# remaining working week
if [ $(($left_workday%5)) -eq 0 ];then
add_weekend=$(($left_workday/5))
else
add_weekend=$(($left_workday/5+1))
fi
result=$(($firstweek_workday + $left_workday +2*$add_weekend))
else
# remaining working day<0
result=$str
fi
# the first day of the current month is Sun
elif [ $weekday -eq 0 ];then
add_day=1
left_workday=$str
if [ $(($left_workday%5)) -eq 0 ];then
add_weekend=$(($left_workday/5-1))
else
add_weekend=$(($left_workday/5))
fi
result=$(($add_day + $left_workday +2*$add_weekend))
# the first day of the current month is Sat
elif [ $weekday -eq 6 ];then
add_day=2
left_workday=$str
if [ $(($left_workday%5)) -eq 0 ];then
add_weekend=$(($left_workday/5-1))
else
add_weekend=$(($left_workday/5))
fi
result=$(($add_day + $left_workday +2*$add_weekend))
fi
# if result lager then the last day
nextmonth=`date -d "+1 month" +%Y-%m-01`
enddate=`date -d"$nextmonth last day" +%d`
if [ $result -le $enddate ];then
result_list+=($result)
fi
done
echo $result_list
if [ $result_list ];then
echo "Excution Days : ${result_list[@]}"
if [[ ${result_list[@]/$(date -d "now" +%d)/} != ${result_list[@]} ]];then
echo "Today is Excution Day"
else
echo "Today is not Excution Day"
exit 1
fi
else
echo "Error input n = $n"
exit 1
fi
使用方法:
./test.sh -n "2 3 5" -d "20211101"
-n 后传list,用空格隔开。判断今日是否是business day 第2 ,3, 5 天 。可以设置任意多天
-d 可以不传值。默认是当前月1号为第一天,可以指定某个月的第一天作为计算月。例如20210801