shell指令碼判斷日期是否合法

2021-10-23 11:21:10 字數 3150 閱讀 5331

就是判斷日期是不是真實的日期,比如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

fiif [ $ -ne 8 ];then

action "日期不合法!" /bin/false

exit

fiyear=$(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

fielif [ $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

fielse

if [ $day -gt 0 -a $day -le 28 ];then

action "日期合法!" /bin/true

else

action "日期不合法!" /bin/false

fifi

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

fielse

if [ $day -gt 0 -a $day -le 28 ];then

action "日期合法!" /bin/true

else

action "日期不合法!" /bin/false

fifi

fielif [ $month -eq 3 ];then

if [ $day -gt 0 -a $day -le 31 ];then

action "日期合法!" /bin/true

else

action "日期不合法!" /bin/false

fielif [ $month -eq 4 ];then

if [ $day -gt 0 -a $day -le 30 ];then

action "日期合法!" /bin/true

else

action "日期不合法!" /bin/false

fielif [ $month -eq 5 ];then

if [ $day -gt 0 -a $day -le 31 ];then

action "日期合法!" /bin/true

else

action "日期不合法!" /bin/false

fielif [ $month -eq 6 ];then

if [ $day -gt 0 -a $day -le 30 ];then

action "日期合法!" /bin/true

else

action "日期不合法!" /bin/false

fielif [ $month -eq 7 ];then

if [ $day -gt 0 -a $day -le 31 ];then

action "日期合法!" /bin/true

else

action "日期不合法!" /bin/false

fielif [ $month -eq 08 ];then

if [ $day -gt 0 -a $day -le 31 ];then

action "日期合法!" /bin/true

else

action "日期不合法!" /bin/false

fielif [ $month -eq 09 ];then

if [ $day -gt 0 -a $day -le 30 ];then

action "日期合法!" /bin/true

else

action "日期不合法!" /bin/false

fielif [ $month -eq 10 ];then

if [ $day -gt 0 -a $day -le 31 ];then

action "日期合法!" /bin/true

else

action "日期不合法!" /bin/false

fielif [ $month -eq 11 ];then

if [ $day -gt 0 -a $day -le 30 ];then

action "日期合法!" /bin/true

else

action "日期不合法!" /bin/false

fielif [ $month -eq 12 ];then

if [ $day -gt 0 -a $day -le 31 ];then

action "日期合法!" /bin/true

else

action "日期不合法!" /bin/false

fielse

action "日期不合法!" /bin/false

fi

Java判斷日期是否合法

記錄一下專案中遇到的乙個bug,當時原有工具類中的 如下 public static boolean isdate string str catch exception e return result 當輸入字串為2018 02 31時返回true,日期預設轉換為了2018 03 03,未對時間格式...

Shell指令碼判斷IP位址是否合法的方法

使用shell校驗ip位址合法性 使用方法 複製 如下 root yang python bash check ip.sh ip位址 執行結果 返回值0校驗合法,非0不合法。shell 複製 如下 root程式設計客棧 yang python vi check ip.sh usr bin sh ch...

判斷日期是否合法的方法

public static boolean chkdateformat string date int year integer.parseint date.substring 0,4 int month integer.parseint date.substring 4,6 1 int day i...