Validating the input date format

I have one script.for that the inputs are fromdate(dd/mon/yyyy) and todate(dd/mon/yyyy).
How i can validate the input format?for eg.27/08/2008 is not valid.27/aug/2008 or 27/Aug/2008 are valid.
and the todate is optional.so if the todate is not present in the input then i need to assign the current date as the
todate in the format-dd/mon/yyyy.how to do this.

What have you tried so far? As a start, you can use case/esac to check the input.

Could you please tell me how I can implement this?

So you have tried nothing so far on your own?

Not much I guess - here is a starting script but I will not write it to the end for you :slight_smile:

#!/usr/bin/ksh

CUR_DATE=`date +%d/%b/%Y`

if [[ -z $2 ]]; then
        END_DATE=${CUR_DATE}
else
        END_DATE=$2
fi

for MY_ARG in $@; do
        case ${MY_ARG} in
                [0-9][0-9]/[a-zA-Z][a-zA-Z][a-zA-Z]/[0-9][0-9][0-9][0-9])       echo "ok";;
                *)                                                              echo "not ok";;
        esac
done

exit 0

As zaxxon already implied, you should show some effort to solve the problem yourself. We liek to help, but we dislike doing others works.

Having said this, it can be difficult to plan such a task, so here is, how you should work out your solution:

1) we observe, that a "date" is a structured entity: we expect one or two digits first, than a slash, then a string out of a fixed number of strings (the month), again a slash and then another four-digit number.

2) lets first check the simplest parts: are 2 slashes in the input? are they separating 3 strings? Are all the characters out of the set of characters we expect? (For instance, we expect only digits and the letters contained in month names. If the imput contains an "X" or a "?" we can conclude its illegal without further investigation. If the input looks like "...//..." (no 3 strings) we can conclude the same. All these checks can be done via simple sed scripts.

3) If the input passed this test(s), we can break it up into its parts and analyze them separately: a day number of "32" is certainly wrong, "31" might be right depending on the month and "29" (for February) might be wrong depending on the year.

4) When checking the month part you might want to translate "08" to "Aug" instead of rejecting it, the work is the same and it broadens the "input tolerance" of your script.

I think you got the drift now, so start with zaxxon has given to you (which is an excellent starting point, btw.) and work through these suggestions, then come back when you still need help. We'll be glad to support you with some details.

I hope this helps.

bakunin

Also note that this is a question which has been asked before; have you tried searching the forums? At the bottom of this page is a listing of similar threads (though it's apparently mainly based on subject lines, so not always very useful).