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

Posted on 2006-03-22
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
    LVL 51

    Accepted Solution

    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

    Thanks, worked like a charm!

    Author Comment

    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"
    LVL 51

    Expert Comment

    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

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

    Expert Comment

    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

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    iSeries FTP Exit Program 8 99
    isEverywhere  challenge 19 48
    abstract class with all non abstract mentods 6 58
    Sed question 2 25
    Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
    If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
    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.
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.

    730 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

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now