- 论坛徽章:
- 0
|
题目:使用一个 if/then/elif/else 结构来打印当前月份。这个脚本应该能打印本月的天数,而且如果当前月是二月的话能给出关于闰年的相关信息。
自己的代码太烦啦,希望高手给个简单的。
自己的代码如下:
[root@localhost shell]# cat monthtest.sh
#!/bin/bash
#This script prints the month information.If it is a leap year,please print the messages.
month=`date +%m`
year=`date +%Y`
if [ $month -eq "01" ];then
echo "The month is $month.This month has 31 days."
elif [ $month -eq "02" ];then
if (( ("$year" % 400)=="0" )) || (( ("$year" % 4 == "0" ) && ("$year" % 100 != "0") ));then
echo "This is a leap year.February has 29 days."
else
echo "This is a not leap year.February has 28 days."
fi
echo "The month is $month.This month has 28 days."
elif [ $month -eq "03" ];then
echo "The month is $month.This month has 31 days."
elif [ $month -eq "04" ];then
echo "The month is $month.This month has 30 days."
elif [ $month -eq "05" ];then
echo "The month is $month.This month has 31 days."
elif [ $month -eq "06" ];then
echo "The month is $month.This month has 30 days."
elif [ $month -eq "07" ];then
echo "The month is $month.This month has 31 days."
elif [ $month -eq "08" ];then
echo "The month is $month.This month has 31 days."
elif [ $month -eq "09" ];then
echo "The month is $month.This month has 30 days."
elif [ $month -eq "10" ];then
echo "The month is $month.This month has 31 days."
elif [ $month -eq "11" ];then
echo "The month is $month.This month has 30 days."
elif [ $month -eq "12" ];then
echo "The month is $month.This month has 31 days."
fi |
|