1831647 Members
2046 Online
110029 Solutions
New Discussion

checking the input

 
mahesh_24
Occasional Advisor

checking the input

Hi,
I am taking a input date ( in the format mm/dd/yy or mm-dd-yy from the keyborad).Now i have written a pattern to match the same which is as follows

[0-1][0-9][-/][0-3][0-9][-/][0-9][0-9]

Now i want to compare that whatever is entered from keyboard is compying to the pattern given below. Any ideas how it can be done using if .....else.
1 REPLY 1
Rodney Hills
Honored Contributor

Re: checking the input

Instead of "if ... else" how about "case". Case can do selection based on simple regular expressions (filename expansion *?[])


mydate="01-02-03"
valid=0
case $mydate in
[0-1][0-9][-/][0-3][0-9][-/][0-9][0-9]) valid=1;;
esac
if [ "$valid" ] ; then
echo "good date"
else
echo "bad date"
fi

HTH

-- Rod Hills
There be dragons...