zoukankan      html  css  js  c++  java
  • 检查日期合法性脚本(转)

    http://bash.cyberciti.biz/time-and-date/validation-shell-script/

    #!/bin/bash
    # Shell program to find the validity of a given date
    # -----------------------------------------------
    # Copyright (c) 2005 nixCraft project <http://cyberciti.biz/fb/>
    # This script is licensed under GNU GPL version 2.0 or above
    # -------------------------------------------------------------------------
    # This script is part of nixCraft shell script collection (NSSC)
    # Visit http://bash.cyberciti.biz/ for more information.
    # -------------------------------------------------------------------------   # store day, month and year
    dd=0
    mm=0
    yy=0   # store number of days in a month
    days=0   # get day, month and year
    echo -n "Enter day (dd) : "
    read dd   echo -n "Enter month (mm) : "
    read mm   echo -n "Enter year (yyyy) : "
    read yy   # if month is negative (<0) or greater than 12
    # then it is invalid month
    if [ $mm -le 0 -o $mm -gt 12 ];
    then
        echo "$mm is invalid month."
        exit 1
    fi   # Find out number of days in given month
    case $mm in
        1) days=31;;
        2) days=28 ;;
        3) days=31 ;;
        4) days=30 ;;
        5) days=31 ;;
        6) days=30 ;;
        7) days=31 ;;
        8) days=31 ;;
        9) days=30 ;;
        10) days=31 ;;
        11) days=30 ;;
        12) days=31 ;;
        *) days=-1;;
    esac   # find out if it is a leap year or not   if [ $mm -eq 2 ]; # if it is feb month then only check of leap year
    then
    	if [ $((yy % 4)) -ne 0 ] ; then
    	   : #  not a leap year : means do nothing and use old value of days
    	elif [ $((yy % 400)) -eq 0 ] ; then
    	   # yes, it's a leap year
    	   days=29
    	elif [ $((yy % 100)) -eq 0 ] ; then
    	   : # not a leap year do nothing and use old value of days
    	else
    	   # it is a leap year
    	   days=29
    	fi
    fi   # if day is negative (<0) and if day is more than
    # that months days then day is invaild
    if [ $dd -le 0 -o $dd -gt $days ];
    then
        echo "$dd day is invalid"
        exit 3
    fi   # if no error that means date dd/mm/yyyy is valid one
    echo "$dd/$mm/$yy is a vaild date"
  • 相关阅读:
    ArcMap+ArcCatalog手工新建点层及添加数据
    zedGraph 图表控件总结(一)
    Android 开发学习笔记(五)—— 最简单的注册界面
    repeater实现删除按钮
    repeater分页实例
    FCKeditor的使用说明
    javascript:void(0)是什么意思
    为asp.net控件点击事件添加Confirm()
    LINQ判断素数
    U盘文件不能删除,怎么处理
  • 原文地址:https://www.cnblogs.com/cqubityj/p/2325857.html
Copyright © 2011-2022 走看看