免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2132 | 回复: 2
打印 上一主题 下一主题

求助:使用一个 if/then/elif/else 结构来打印当前月份 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-07-17 21:06 |只看该作者 |倒序浏览
题目:使用一个 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

论坛徽章:
1
荣誉会员
日期:2011-11-23 16:44:17
2 [报告]
发表于 2007-07-17 23:27 |只看该作者
case是if then 最好的替代

论坛徽章:
0
3 [报告]
发表于 2007-07-18 09:40 |只看该作者
echo `cal` | nawk '{print $NF}'
夠簡單嗎
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP