Eric
Eric

Reputation: 390

Test the date format to make sure it has been input correctly

I'm trying to write a test in bash that will check that a date has been entered correctly (or that a date has been entered at all). Here is what I'm trying to do:

tDate=$(lastCOB)
tDateOkay=0

until [ $tDateOkay -eq 1 ] ; do
    read -p "Please Enter date for search.  Use format: Date (YYYYMMDD): " -e -i "$tdate" tDate
        if [[ -z "$tDate" || {check for valid YYMMDD format}]] ; then
                echo "Invalid date. Please enter date in the correct format."
        elif [[ $tDate -gt $(today)|| $tdate -eq $(today) ]] ; then
                echo "Date must be in the past.  Please try again."
        else
            tDateOkay=1
        fi
done

The date has to be in the past and has to be written in the correct format, or the data won't be pulled from the correct folder. Thanks.

Upvotes: 2

Views: 223

Answers (1)

Zombo
Zombo

Reputation: 1

# other stuff
elif (( `date +%s -d $tDate` >= `date +%s` ))
then
  echo 'Date must be in the past.  Please try again.'
  # other stuff

Upvotes: 1

Related Questions