Validate date format in a shell script

后端 未结 12 1587
我在风中等你
我在风中等你 2020-11-29 11:11

I have to create a Shell Script wherein one of the parameters will be the date in the format dd/mm/yyyy. My question is, how can I check if the Date passed as parameter real

相关标签:
12条回答
  • 2020-11-29 12:00

    I wrote this bash script to validate date. I can accept mont as alphanumeric.

    #!/bin/bash
    
    function isDateValid {
        DATE=$1
    
        if [[ $DATE =~ ^[0-9]{1,2}-[0-9a-zA-Z]{1,3}-[0-9]{4}$ ]]; then
            echo "Date $DATE is a number!"
            day=`echo $DATE | cut -d'-' -f1`
            month=`echo $DATE | cut -d'-' -f2`
            year=`echo $DATE | cut -d'-' -f3`
    
                    if [ "$month" == "01" ] || [ "$month" == "1" ]; then
                            month="Jan"
                    elif [ "$month" == "02" ] || [ "$month" == "2" ]; then
                            month="Feb"
                    elif [ "$month" == "03" ] || [ "$month" == "3" ]; then
                            month="Mar"
                    elif [ "$month" == "04" ] || [ "$month" == "4" ]; then
                            month="Apr"
                    elif [ "$month" == "05" ] || [ "$month" == "5" ]; then
                            month="May"
                    elif [ "$month" == "06" ] || [ "$month" == "6" ]; then
                            month="Jun"
                    elif [ "$month" == "07" ] || [ "$month" == "7" ]; then
                            month="Jul"
                    elif [ "$month" == "08" ] || [ "$month" == "8" ]; then
                            month="Aug"
                    elif [ "$month" == "09" ] || [ "$month" == "9" ]; then
                            month="Sep"
                    elif [ "$month" == "10" ]; then
                            month="Oct"
                    elif [ "$month" == "11" ]; then
                            month="Nov"
                    elif [ "$month" == "12" ]; then
                            month="Dec"
                    fi
    
            ymd=$year"-"$month"-"$day
            echo "ymd: "$ymd
            dmy=$(echo "$ymd" | awk -F- '{ OFS=FS; print $3,$2,$1 }')
            echo "dmy: "$dmy
            if date --date "$dmy" >/dev/null 2>&1; then
                    echo "OK"
                return 0
            else
                    echo "NOK"
                return 1
            fi
        else
            echo "Date $DATE is not a number"
            return 1
        fi
    }
    
    
    if isDateValid "15-15-2014"; then
        echo "date is valid =)"
    else
        echo "bad format date"
    fi
    echo "==================="
    if isDateValid "15-12-2014"; then
            echo "date is valid =)"
    else
            echo "bad format date"
    fi
    echo "==================="
    if isDateValid "15-Dec-2014"; then
            echo "date is valid =)"
    else
            echo "bad format date"
    fi
    echo "==================="
    if isDateValid "1-May-2014"; then
            echo "date is valid =)"
    else
            echo "bad format date"
    fi
    echo "==================="
    if isDateValid "1-1-2014"; then
            echo "date is valid =)"
    else
            echo "bad format date"
    fi
    echo "==================="
    if isDateValid "12-12-2014"; then
            echo "date is valid =)"
    else
            echo "bad format date"
    fi
    
    0 讨论(0)
  • 2020-11-29 12:01

    How about using awk:

    echo "31/12/1999" | awk  -F '/' '{ print ($1 <= 31 && $2 <= 12 && match($3, /^[1-9][1-9][1-9][1-9]$/)) ? "good" : "bad" }'
    

    It prints "good" if its valid date else prints "bad"

    0 讨论(0)
  • 2020-11-29 12:01

    Blockquote

    DATE = "$*"
    
    [[ "${DATE}" != @(((([123][0]|[012][1-9])|3[1])?([ \/._-])(0[13578]|1[02])?([ \/._-])(19|20)[0-9][0-9])|(([123][0]|[012][1-9])?([ \/._-])\
    (0[469]|11)?([ \/._-])(19|20)[0-9][0-9])|(([12][0]|[01][1-9]|2[1-8])?([ \/._-])02?([ \/._-])(19|20)[0-9][0-9])|(29?([ \/._-])02?([ \/._-])\
    (19|20(0[48]|[2468][048]|[13579][26])))) ]] && echo error || echo good)
    
    0 讨论(0)
  • 2020-11-29 12:05

    The simplest solution, that still works perfectly, is the following :

    if [[ $1 =~ ^[0-9]{4}-[0-9]{2}-[0-9]{2}$ ]] && date -d "$1" >/dev/null 2>&1
       ...
    

    It consists in combining 2 checks :

    • the first part checks that $1 is of this format : NNNN-NN-NN
    • the second part checks that it is a valid date

    You need the two checks because :

    • if you don't do the first check, date will exit with code 0 even if your variable is a valid date in another format
    • if you don't do the second check, then you can end up with a 0 even for variables such as 2016-13-45
    0 讨论(0)
  • 2020-11-29 12:05

    `X="2016-04-21" then check for the below value being 1 or 0.

    cal echo $x | cut -c 6-7 echo $x | cut -c 1-4 2>/dev/null | grep -c echo $x | cut -c 9-10

    If the value is 1, then it's valid, else it's not valid.

    0 讨论(0)
  • 2020-11-29 12:08

    Though the solution (if [[ $1 =~ ^[0-9]{4}-[0-9]{2}-[0-9]{2}$ ]] && date -d "$1" >/dev/null 2>&1) of @https://stackoverflow.com/users/2873507/vic-seedoubleyew is best one at least for linux, but it gives error as we can not directly compare/match regex in if statement. We should put the regex in a variable and then we should compare/match that variable in if statement. Moreover second part of if condition does not return a boolean value so this part will also cause error.

    So I have done slight modification in the above formula and this modification can also be customized further for various other formats or combination of them.

    DATEVALUE=2019-11-12
    REGEX='^[0-9]{4}-[0-9]{2}-[0-9]{2}$'
    if [[ $DATEVALUE =~ $REGEX ]] ; then
        date -d $DATEVALUE 
      if [ $? -eq 0 ] ; then
        echo "RIGHT DATE"
       else 
        echo "WRONG DATE"
      fi
    else
     echo "WRONG FORMAT"
    fi
    
    0 讨论(0)
提交回复
热议问题