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

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?

Thanks,

Lisa
lphillips120898Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ahoffmannCommented:
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 ;-)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
lphillips120898Author Commented:
Thanks, worked like a charm!
0
lphillips120898Author Commented:
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.

thanks,

Lisa

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)}'
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

ahoffmannCommented:
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 ;-)
0
lphillips120898Author Commented:
Thank You!  It is easier for me to understand now - and I prefer multiliners  8^)
0
CuthbertDibbleGrubCommented:
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.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.