工作中遇到在shell中判断年份是否为闰年,写了一个shell方法
执行脚本并传入年份参数即可得到结果
#!bin/bash
function isLeapYear(){
year=$1
let "c1=$year % 4"
let "c2=$year % 100"
let "c3=$year % 400"
if [ ! "$c1" -eq 0 ]
then
leap=0
elif [ ! "$c2" -eq 0 ]
then
leap=1
elif [ "$c3" -eq 0 ]
then
leap=1
else
leap=0
fi
echo $leap
}
a=$(isLeapYear $1)
echo "isLeapYear:"$a