专注各种脚本编程
Baidu
加入收藏夹
本站内容有下面分类知识,欢迎您的到来^_^
shell相关:指令篇 基础篇 脚本欣赏 编程实例 shell问问 shell视频教程 技巧篇 水平测试 E文资料 vi编辑器 高级Bash脚本编程指南
其他:mysql perl c语言 oracle
当前位置:| 主页>shell脚本欣赏>

通过输入年月日来计算星期

百度收藏 QQ搜藏

作者: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年以后的所有日期都可以算出星期。

上一篇:天气预报(限国内城市) 下一篇:一个演示汉诺塔的脚本

power by soyo123 2007-2008