6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
下面的脚本用来计算指定年份的母亲节和父亲节的日期。
Bash脚本:calc_date.sh
#!/bin/sh
# 母亲节(每年5月的第二个星期日 )
# usage: mother_day [year]
mother_day()
{
local may1 # 5月1日
if [ "$1" ]; then
may1=$1-05-01 # 也可以是是$1/05/01
else
may1=5/1 # 也可以是05/01,但不能是05-01
fi
#date -d $may1
# 看5月1日是星期几
local w=$(date +%w -d $may1) # %w 0=星期天 1-6=星期一到六
#echo $w
if [ $w -eq 0 ]; then # 如果是5月1日星期天,就跳过一个星期
date +%F -d "$may1 +1 week"
else # 如果5月1日不是星期天,就跳过两个星期,再减去w天
date +%F -d "$may1 +2 week -$w day"
fi
}
# 父亲节(每年6月的第三个星期日)
# usage: father_day [year]
father_day()
{
local june1 # 保存6月1日的日期
if [ "$1" ]; then
june1=$1-06-01
else
june1=6/1
fi
# 因为采用1-7表示星期几,简化了计算逻辑
local w=$(date +%u -d $june1) # %u 7=星期天,1-6=星期一到六
date +%F -d "$june1 +3 week -$w day"
}
# usage: ./calc_date.sh [year]
if [ "$1" ]; then
echo Mother Day of year $1 is $(mother_day "$1")
echo Father Day of year $1 is $(father_day "$1")
else
echo Mother Day of this year is $(mother_day)
echo Father Day of this year is $(father_day)








