Validate date format in a shell script

后端 未结 12 1586
我在风中等你
我在风中等你 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 11:42

    Simplest way for dd/mm/yyyy exactly in Bash is:

    if [[ $1 == [0-3][0-9]/[0-1][0-9]/[0-9][0-9][0-9][0-9] ]]
    

    Or

    if [[ $1 =~ ^[0-3][0-9]/[0-1][0-9]/[0-9]{4}$ ]]
    
    0 讨论(0)
  • 2020-11-29 11:44

    Here's a function to do some data validation this:

    # Script expecting a Date parameter in MM-DD-YYYY format as input
    verifyInputDate(){
        echo ${date} | grep '^[0-9][0-9]-[0-9][0-9]-[0-9][0-9][0-9][0-9]$'
        if [ $? -eq 0 ]; then
             echo "Date is valid"
         else
              echo "Date is not valid"
         fi
    }
    
    0 讨论(0)
  • 2020-11-29 11:47

    Use date

    date "+%d/%m/%Y" -d "09/99/2013" > /dev/null  2>&1
     is_valid=$?
    

    If you do not get 0 then date is in invalid format.

    0 讨论(0)
  • 2020-11-29 11:47

    This function expects 2 strings,a format string, a date string

    The format string uses the codes from the date command but does not include the '+'

    The function returns 0 if the provided date matches the given format, otherwise it returns 1

    Code (my_script.sh)

    #!/bin/bash
    
    datecheck() {
        local format="$1" d="$2"
        [[ "$(date "+$format" -d "$d" 2>/dev/null)" == "$d" ]]
    }
    
    date_test="$1"
    
    echo $date_test
    if datecheck "%d %b %Y" "$date_test"; then
        echo OK
    else
        echo KO
    fi
    

    Output

    $ ./my_script.sh "05 Apr 2020"
    05 Apr 2020
    OK
    $ ./my_script.sh "foo bar"
    foo bar
    KO
    
    0 讨论(0)
  • 2020-11-29 11:48

    First, check the form of the input using the regex. Then use awk to switch to mm/dd/yyyy and use date to validate. You can use the following expression in your if statement:

    echo "$1" | egrep -q '^[0-3][0-9]/[0-1][0-9]/[0-9]{4}$' && date -d "$(echo "$1" | awk 'BEGIN{FS=OFS="/"}{print $2"/"$1"/"$3}')" >/dev/null 2>&1
    
    0 讨论(0)
  • #! /bin/bash
    
    isDateInvalid()
    {
        DATE="${1}"
    
        # Autorized separator char ['space', '/', '.', '_', '-']
        SEPAR="([ \/._-])?"
    
        # Date format day[01..31], month[01,03,05,07,08,10,12], year[1900..2099]
        DATE_1="((([123][0]|[012][1-9])|3[1])${SEPAR}(0[13578]|1[02])${SEPAR}(19|20)[0-9][0-9])"
    
        # Date format day[01..30], month[04,06,09,11], year[1900..2099]
        DATE_2="(([123][0]|[012][1-9])${SEPAR}(0[469]|11)${SEPAR}(19|20)[0-9][0-9])"
    
        # Date format day[01..28], month[02], year[1900..2099]
        DATE_3="(([12][0]|[01][1-9]|2[1-8])${SEPAR}02${SEPAR}(19|20)[0-9][0-9])"
    
        # Date format day[29], month[02], year[1904..2096]
        DATE_4="(29${SEPAR}02${SEPAR}(19|20(0[48]|[2468][048]|[13579][26])))"
    
        # Match the date in the Regex
    
        if ! [[ "${DATE}" =~ "^(${DATE_1}|${DATE_2}|${DATE_3}|${DATE_4})$" ]]
        then
            echo -e "ERROR - '${DATE}' invalid!"
        else
            echo "${DATE} is valid"
        fi
    }
    
    echo
    echo "Exp 1: "`isDateInvalid '12/13/3000'`
    echo "Exp 2: "`isDateInvalid '12/11/2014'`
    echo "Exp 3: "`isDateInvalid '12 01 2000'`
    echo "Exp 4: "`isDateInvalid '28-02-2014'`
    echo "Exp 5: "`isDateInvalid '12_02_2002'` 
    echo "Exp 6: "`isDateInvalid '12.10.2099'`
    echo "Exp 7: "`isDateInvalid '31/11/2000'`
    
    0 讨论(0)
提交回复
热议问题