写一个Bash脚本来计算母亲节和父亲节的日期
母亲节(每年5月的第二个星期日
)
2005年5月8日
2006年5月14日
2007年5月13日
2008年5月11日
2009年5月10日
2010年5月9日
2011年5月8日
2012年5月13日
父亲节(6月第三个星期日),下面是最近几年的父亲节日期
2005年6月19日
2006年6月18日
2007年6月17日
2008年6月15日
2009年6月21日
2010年6月20日
2011年6月19日
2012年6月17日
下面是Linux查看某年某月的日历的方式。
[root@node56 ~]# cal 5 2012
五月 2012
日 一 二 三 四 五 六
1 2 3 4 5
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)
fi
[root@node56 ~]# ./calc_date.sh
Mother Day of this year is 2011-05-08
Father Day of this year is 2011-06-19
[root@node56 ~]# ./calc_date.sh 2011
Mother Day of year 2011 is 2011-05-08
Father Day of year 2011 is 2011-06-19
[root@node56 ~]# ./calc_date.sh 2010
Mother Day of year 2010 is 2010-05-09
Father Day of year 2010 is 2010-06-20
[root@node56 ~]# ./calc_date.sh 2009
Mother Day of year 2009 is 2009-05-10
Father Day of year 2009 is 2009-06-21
[root@node56 ~]# ./calc_date.sh 2008
Mother Day of year 2008 is 2008-05-11
Father Day of year 2008 is 2008-06-15
[root@node56 ~]# ./calc_date.sh 2007
Mother Day of year 2007 is 2007-05-13
Father Day of year 2007 is 2007-06-17
[root@node56 ~]# ./calc_date.sh 2006
Mother Day of year 2006 is 2006-05-14
Father Day of year 2006 is 2006-06-18
[root@node56 ~]# ./calc_date.sh 2005
Mother Day of year 2005 is 2005-05-08
Father Day of year 2005 is 2005-06-19
[root@node56 ~]# ./calc_date.sh 2012
Mother Day of year 2012 is 2012-05-13
Father Day of year 2012 is 2012-06-17
分享到:
相关推荐
福州大学linux作业:编写一个Bash脚本,它可以打印出下面的菜单,并提示输入一个字符来选择其中的一个选项,菜单如下。。。
Linux命令行和bash脚本编程圣经 Linux命令行和bash脚本编程圣经
bash脚本编写教程简易教 bash脚本编写教程简易教
高级Bash脚本编程指南 操作系统 - Linux - 高级Bash脚本编程指南.zip
高级Bash脚本编程指南
一个很不错的bash脚本编写教程,至少没接触过BASH的也能看懂!
高级Bash脚本编程指南(Advanced Bash-Scripting Guide)6.4版本的shell脚本源码
bash脚本的教程,很详细,共享一下。 本指南不期望读者有任何的脚本和编程的背景知识,...教程中的练习和很好的注释能激起读者积极参与,但前提是要认识到:只有真正动手去写脚本才是唯一真正学会脚本编程技术的方法.
高级bash脚本编程指南下载,欢迎大家喜欢
Shell 脚本编程 是你在 Linux 下学习或练习编程的最简单的方式。尤其对 系统管理员要处理着自动化任务,且要开发新的简单的实用程序或工具等(这里只是仅...本文中,我们将分享 9 个写出高效可靠的 bash 脚本的实用技巧
高级Bash 脚本编程指南 高级Bash 脚本编程指南
一个bash脚本,旨在使用基本参数解析创建其他bash脚本
高级Bash脚本编程指南高级Bash脚本编程指南.pdf高级Bash脚本编程指南.pdf
Advanced-Bash-Scripting-Guide-in-Chinese 《高级Bash脚本编程指南》revision 10 中文第10版
高级bash脚本指导 高级bash脚本指导 高级bash脚本指导 高级bash脚本指导
高级 bash 脚本 编程指南 6.0 最新版 2009 高级 bash 脚本 编程指南 6.0 最新版 2009 高级 bash 脚本 编程指南 6.0 最新版 2009
高级Bash脚本编程指南,chm格式。