专注各种脚本编程
Baidu
加入收藏夹
本站内容有下面分类知识,欢迎您的到来^_^
shell相关:指令篇 基础篇 脚本欣赏 编程实例 shell问问 shell视频教程 技巧篇 水平测试 E文资料 vi编辑器 高级Bash脚本编程指南
其他:mysql perl c语言 oracle
当前位置:| 主页>shell脚本欣赏>

shell脚本检查输入年份是否是闰年

百度收藏 QQ搜藏

原文: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
上一篇:shell脚本检查输入日期是否正确 下一篇:shell脚本 当用户进入系统时显示问候语

power by soyo123 2007-2008