You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

43 lines
1005 B

6 years ago
#!/bin/bash
6 years ago
: '
方法一
6 years ago
LeapYear=`date +%Y`
6 years ago
if [ $[ $LeapYear % 400 ] -eq 0 ] ; then
echo "闰年"
elif [ $[ $LeapYear % 4 ] -eq 0 ] ; then
if [ $[ $LeapYear % 100 ] -ne 0 ] ; then
echo "闰年"
else
echo "不是闰年"
fi
else
echo "不是闰年"
fi
'
6 years ago
6 years ago
: '
6 years ago
# 方法二,利用布尔运算实现
6 years ago
LeapYear=`date +%Y`
if (( ( "$LeapYear" % 400 ) == "0" )) || (( ( "$LeapYear" % 4 == 0 ) && ( "$LeapYear" % 100 != "0" ) )) ; then
6 years ago
echo "闰年"
else
echo "不是闰年"
fi
6 years ago
# 这相当于let语句。如果你尝试类似$ [$ year400]的东西,你会在这里使用方括号卡住,因为在这里,方括号本身并不代表实际的命令。
'
# 利用read命令进行交互式判断实例
echo "请输入您想要判断是否为闰年的日期(四个数字),然后按enter"
read LeapYear
if (( ("$LeapYear" % 400 ) == 0 )) || (( ("$LeapYear" % 4 == 0 ) && ( "$LeapYear" % 100 != 0 ) )); then
echo "闰年"
else
echo "不是闰年"
fi