原文:http://bash.cyberciti.biz/time-and-date/find-whether-year-ls-leap-or-not/
解释下闰年:闰年是为了弥补因人为历法规定造成的年度天数与地球实际公转周期的时间差而设立的。补上时间差的年份,即有闰日的年份为闰年
公历闰年判定遵循的规律为: 四年一闰,百年不闰,四百年再闰.
公历闰年的简单计算方法(符合以下条件之一的年份即为闰年) 1.能被4整除而不能被100整除。 2.能被400整除。
下面来看下该shell脚本的源代码:
#!/bin/bash
# Shell program to read any year and find whether leap year or not
# -----------------------------------------------
# Copyright (c) 2005 nixCraft project <http://cyberciti.biz/fb/>
# This script is licensed under GNU GPL version 2.0 or above
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit http://bash.cyberciti.biz/ for more information.
# -------------------------------------------------------------------------
# store year
yy=0
isleap="false"
echo -n "输入年份(yyyy) : "
read yy
# find out if it is a leap year or not
if [ $((yy % 4)) -ne 0 ] ; then
: # not a leap year : means do nothing and use old value of isleap
elif [ $((yy % 400)) -eq 0 ] ; then
# yes, it's a leap year
isleap="true"
elif [ $((yy % 100)) -eq 0 ] ; then
: # not a leap year do nothing and use old value of isleap
else
# it is a leap year
isleap="true"
fi
if [ "$isleap" == "true" ];
then
echo "$yy 是闰年"
else
echo "$yy 不是闰年"
fi |