博客首页 注册 建议与交流 排行榜 加入友情链接
推荐 投诉 搜索: 帮助

剑心通明的资料库

文章均为转载,本人不负因参考它所导致的一切后果,请谨慎参考!如您的文章不愿被转载,请点击此处联系本人!
  jxtm.cublog.cn

关于作者
姓名:剑心通明
职业:高级工程师(专修灵魂^_^)
年龄:20出头30不到
位置:网络上一节点
个性介绍:努力学习每一天!
倾心打造:http://www.bsdlover.cn
http://bbs.bsdlover.cn
BSD爱好者的乐园!
|| << >> ||
我的分类


通过输入年月日来计算星期
作者:dunerunner
代码:
#!/bin/bash
# week
#QQ:82386723  E-mail:sc_fan@163.com

value=year

error()
{
echo $value error! please input again.
continue
}

while
    read -p "Please input $value: " tmp
do
       if [ "$tmp" ]; then
        if [ `echo $tmp | awk '{if($0~/[^0-9]/) print "1"}'` ]; then
    echo $value error! please input again.
           continue
fi
else
    error
       fi
      
       case $value in
         year)
     value=month
     year=$tmp
     continue
  ;;
         month)
     if [ $tmp -gt 0 -a $tmp -le 12 ]; then
        value=day
        month=$tmp
        continue
      else
  error
            fi
  ;;
  day)
     case $month in
       1|3|5|7|8|10|12)
          if [ $tmp -gt 0 -a $tmp -le 31 ]; then
             day=$tmp
      break
    else
              error
   fi
       ;;
       4|6|9|11)
          if [ $tmp -gt 0 -a $tmp -le 30 ]; then
      day=$tmp
      break
    else
       error
   fi
              ;;
       2)
          case $[year%4] in
     0)
        if [ $tmp -gt 0 -a $tmp -le 29 ]; then
           day=$tmp
    break
         else
            echo $value error! $year is leap year, input again.
     continue
        fi
     ;;
     *)
        if [ $tmp -gt 0 -a $tmp -le 28 ]; then
           day=$tmp
    break
         else
            error
        fi
     ;;
   esac
       ;;
     esac
  ;;
       esac
done

century=$[year/100+1]
declare -i tmp=0

for (( i = 0; i < $[year%28]; i++))
do  if [ $[i%4] = 0 ]; then
       tmp=$[tmp+366]
     else
        tmp=$[tmp+365]
    fi
done

for (( i = 1; i < $month; i++))
do  case $i in
      1|3|5|7|8|10|12)
         tmp=$[tmp+31]
      ;;
      4|6|9|11)
         tmp=$[tmp+30]
      ;;
      2)
         if [ $[year%4] = 0 ]; then
     tmp=$[tmp+29]
   else
      tmp=$[tmp+28]
  fi
      ;;
    esac
done

tmp=$[(tmp+day)%7]
week=(Thursday Friday Saturday Sunday Monday Tuesday Wednesday)
week=${week[tmp]}

echo -e "\nCentury $century $year.$month.$day $week\n"

本人使用linux半年多了,水平不济可能有比较罗嗦的语句,望各位不要见笑,还请大家多多指教。
上面的脚本是我自己根据一些规律写出来的,加入了点判定,如果输入字母和符号等错误会提示,从公元0年以后的所有日期都可以算出星期

 原文地址 http://www.bsdlover.cn/html/40/n-840.html
发表于: 2008-05-03,修改于: 2008-05-03 11:05,已浏览377次,有评论0条 推荐 投诉


网友评论
 发表评论