Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


shell script - check for entering date in the correct format on the command line

Posted on 2006-03-22
Medium Priority
Last Modified: 2013-12-26
I'm writing a script that needs to have the date passed as a argument on the commnad line.  The date must be entered in the correct format, which in this case is, MM-DD-YYYY.  This also need to check to see if they even entered the date on the command line.

Does anyone have a snippet of code that will perform that check?


Question by:lphillips120898
  • 3
  • 2
LVL 51

Accepted Solution

ahoffmann earned 500 total points
ID: 16258024
assuming programming in sh, use something like:

echo "$1" | awk -F- '($1!~/[0-1][0-9]/){print "bad month";exit(1)}($2!~/[0-3][0-9]/){print "bad day";exit(1)}($3!~/[1-9][0-9][0-9][0-9]/){print "bad year";exit(1)}'
[ $? -ne 0 ] && exit 1

# regex in awk to be improved in many ways ;-)

Author Comment

ID: 16259182
Thanks, worked like a charm!

Author Comment

ID: 16259331
question - I put the code on separate lines to make it easier to read, breaking at the ";"  Would you explain how the scripts works?  If it's too much trouble for the amount of points I understand.



echo "$MDATE" | awk -F- '($1!~/[0-1][0-9]/){print "bad month"
exit(1)}($2!~/[0-3][0-9]/){print "bad day"
exit(1)}($3!~/[1-9][0-9][0-9][0-9]/){print "bad year"
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

LVL 51

Expert Comment

ID: 16260721
awk splits each line (only one should reach) at the - into words (-F- option), then checks the words ($1, $2, $3) using a simple regex. If the regex does not macth it prints the message and exits awk with an status code 1.
My sample uses awk's status code to exit the script too:  [ $? -ne 0 ]
Take care that the suggested regex are lazy, it allows 19 as month ...

> .. put the code on separate lines ..
take care that this depends on your version of the shell, a human reader friendly version might be:

echo "$MDATE" | awk -F- '
  ($1!~/[0-1][0-9]/){ print "bad month"; exit(1) }
  ($2!~/[0-3][0-9]/){ print "bad day"; exit(1) }
  ($3!~/[1-9][0-9][0-9][0-9]/){ print "bad year"; exit(1) }

but I prefer onliners ;-)

Author Comment

ID: 16268429
Thank You!  It is easier for me to understand now - and I prefer multiliners  8^)

Expert Comment

ID: 16755312
Our Solaris boxes have a ckdate shell command which does exactly what you're after, so could be incorporated into your script easily if available.  I mention this as the accepted answer above will not throw out 02/33/2006, 19/19/1999, 02/31/2006 etc.  ckdate will detect leap years as well.

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…

581 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question