shell脚本判断日期是否合法

用shell脚本判断输入的日期是否合法

就是判断日期是不是真实的日期,比如20191115就是合法的日期,20191132就是不合法的

#!/bin/bash
[ -f /etc/init.d/functions ] && source /etc/init.d/functions || echo "函数库文件不存在!"
read -p "请输入一个纯8位数字组成的日期:" Date
if [[ ! $Date =~ ^[0-9]+$ ]];then
    action "日期不合法!" /bin/false
    exit
fi
if [ ${#Date} -ne 8 ];then
    action "日期不合法!" /bin/false
    exit
fi
Year=$(echo $Date |cut -c 1-4)
Month=$(echo $Date |cut -c 5-6)
Day=$(echo $Date |cut -c 7-8)
if [ $Month -eq 1 ];then
    if [ $Day -gt 0 -a $Day -le 31 ];then
        action "日期合法!" /bin/true
    else
        action "日期不合法!" /bin/false
    fi
elif [ $Month -eq 2 ];then
    S=$(echo $Date |cut -c 3-4)
    if [ $S -eq 0 ];then
        #世纪年(xx00)需要被400整除才是闰年
        R=$(($Year % 400))
        if [ $R -eq 0 ];then
            if [ $Day -gt 0 -a $Day -le 29 ];then
                action "日期合法!" /bin/true
            else
                action "日期不合法!" /bin/false
            fi
        else
            if [ $Day -gt 0 -a $Day -le 28 ];then
                action "日期合法!" /bin/true
            else
                action "日期不合法!" /bin/false
            fi
        fi
    else
        #非世纪年只要能被4整除就是闰年
        N=$(($Year % 4))
        if [ $N -eq 0 ];then
            if [ $Day -gt 0 -a $Day -le 29 ];then
                action "日期合法!" /bin/true
            else
                action "日期不合法!" /bin/false
            fi
        else
            if [ $Day -gt 0 -a $Day -le 28 ];then
                action "日期合法!" /bin/true
            else
                action "日期不合法!" /bin/false
            fi
        fi
    fi
elif [ $Month -eq 3 ];then
    if [ $Day -gt 0 -a $Day -le 31 ];then
        action "日期合法!" /bin/true
    else
        action "日期不合法!" /bin/false
    fi
elif [ $Month -eq 4 ];then
    if [ $Day -gt 0 -a $Day -le 30 ];then
        action "日期合法!" /bin/true
    else
        action "日期不合法!" /bin/false
    fi
elif [ $Month -eq 5 ];then
    if [ $Day -gt 0 -a $Day -le 31 ];then
        action "日期合法!" /bin/true
    else
        action "日期不合法!" /bin/false
    fi
elif [ $Month -eq 6 ];then
    if [ $Day -gt 0 -a $Day -le 30 ];then
        action "日期合法!" /bin/true
    else
        action "日期不合法!" /bin/false
    fi
elif [ $Month -eq 7 ];then
    if [ $Day -gt 0 -a $Day -le 31 ];then
        action "日期合法!" /bin/true
    else
        action "日期不合法!" /bin/false
    fi
elif [ $Month -eq 08 ];then
    if [ $Day -gt 0 -a $Day -le 31 ];then
        action "日期合法!" /bin/true
    else
        action "日期不合法!" /bin/false
    fi
elif [ $Month -eq 09 ];then
    if [ $Day -gt 0 -a $Day -le 30 ];then
        action "日期合法!" /bin/true
    else
        action "日期不合法!" /bin/false
    fi
elif [ $Month -eq 10 ];then
    if [ $Day -gt 0 -a $Day -le 31 ];then
        action "日期合法!" /bin/true
    else
        action "日期不合法!" /bin/false
    fi
elif [ $Month -eq 11 ];then
    if [ $Day -gt 0 -a $Day -le 30 ];then
        action "日期合法!" /bin/true
    else
        action "日期不合法!" /bin/false
    fi
elif [ $Month -eq 12 ];then
    if [ $Day -gt 0 -a $Day -le 31 ];then
        action "日期合法!" /bin/true
    else
        action "日期不合法!" /bin/false
    fi
else
    action "日期不合法!" /bin/false
fi

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

冻灬结

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值