judge_leap_year.sh
#!/bin/bash
# 能被4整除并且并不能被100整除的年份是闰年
# 能被400整除的年份也是闰年
read -p "Please input one year:" year
if [ "$year" = "" ];then
echo "You do not input year"
exit
fi
if [[ "$year" =~ [a-Z] ]];then
echo "You do not input number"
exit
fi
if [ $[year % 4] -eq 0 ] && [ $[year % 100] -ne 0];then
echo "$year is leap year"
elif [ $[year % 400] -eq 0 ];then
echo "$year is leap year"
else
echo "$year is not leap year"
fi
验证:
[root@logstash ~]# sh judge_leap_year.sh
Please input one year:
You do not input year
[root@logstash ~]# sh judge_leap_year.sh
Please input one year:dafa
You do not input number
[root@logstash ~]# sh judge_leap_year.sh
Please input one year:20e
You do not input number
[root@logstash ~]#