regex - Bash shell: How to check for specific date format? -


i have bash shell script checks see if shell variable contains number:

   if ! [[ "$step" =~ ^[0-9]+$ ]]         exec >&2; echo "error: $step not step number.";      exit 1    fi 

now need similar check see if variable contains date in required format yyyy-mm-dd (example: today 2013-05-13) dashes. how can done regular expression in bash shell or need external program this?

regex not right tool job.

e.g.

2013-02-29 (invalid date) 2012-02-29 (valid date) 2013-10-31 (valid date) 2013-09-31 (invalid date) ... 

i suggest passing string date -d, check return value. if return 0, fine. if return 1, invalid date.

for example:

kent$  date -d "2012-02-29" > /dev/null 2>&1 kent$  echo $? 0  kent$  date -d "2013-02-29" > /dev/null 2>&1 kent$  echo $? 1 

if want force format yyyy-mm-dd can both regex , date validation. regex format, , date date validation.

because date -d accepts string 02/27/2012 too.


Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -