I wrote this small Unix shell script to validate the date format. My script should check whether the string is in YYYYMMDDHHMMSS format. If it is not in this format, it should send an error message.
For example, I declared an variable a
and assigned a value to it.
a=20150620223405
date "+%Y%m%d%H%M%S" -d $a > /dev/null 2>&1
if [ $? -ne 0 ]
then
echo "Invalid format"
else
echo "Valid format"
fi
It always shows as "Invalid format", I want to know what is the mistake here and how to proceed.
From
man date
:And about format:
This means that you cannot validate the date you pass as an argument.
date
validates it by itself regardless of format you supplied (as man says that FORMAT controls the output, not the input).To validate the date i'd suggest you to use a regular expression and
grep (1)
.