Register
It is currently Thu Aug 21, 2014 11:52 pm

Comparing or matching numbers.


All times are UTC - 6 hours


Post new topic Reply to topic  [ 11 posts ] 
Author Message
 PostPosted: Wed May 19, 2010 10:15 am   

Joined: Wed May 19, 2010 9:56 am
Posts: 21
I thought this would be easy, but obviously, I am missing something here.

I have a bash script where I want the user to enter a date on the command line in the format DD-MMM-YYYY, lets use 19-MAY-2010

Now I want to check to make sure they enter a correctly format date.

I have tried everything I can find online to get this to work.
First I get the DD part and put it in a variable: SDF1=`echo $1 | awk -F- '{print $1}'
That successfully gets me a two digit 19 in the SDF1 variable.

Now here is where my headache begins. (:| I can't figure out how to setup the pattern matching so that a two digit number is matched and evaluated as being valid or invalid i.e. a two digit day or not.

I have tried if all sorts of versions of [0-3][0-3] or [0-3]\{2\}
I have tried all sorts of quoting and delimiting with logical and matching operators like -ne, -eq, !=
I've echoed the $SDF1 and piped it to grep and used '[0-3]\{2\}" and stuff like that.
Scripting would go something like this, obviously it is wrong, but what I tried recently:

if [[ "$SDF1" =~ [0-3]{\2\} ]]; then
echo ok
else
echo not ok
fi

This replys that 19: "not ok"

Can someone help me get the 19 or DD to be evaluated if it is a number?

Of course I am going to have to do the same with the MMM and the YYYY.

I sincerely appreciate any and all help that may come.

Regards
George


Top
 Profile  
 PostPosted: Wed May 19, 2010 12:43 pm   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 535
if you have GNU/date, you can check date is valid against date -d'YYYY-MM-DD'


Top
 Profile  
 PostPosted: Thu May 20, 2010 6:14 am   

Joined: Wed May 19, 2010 9:56 am
Posts: 21
Watael brings up an excellent point. Maybe I am approaching this wrong. If anyone has an idea for a better way to handle a DD-MMM-YYYY situation, I'd love to hear from you.

Thanks


Top
 Profile  
 PostPosted: Thu May 20, 2010 8:32 am   

Joined: Wed May 19, 2010 9:56 am
Posts: 21
Oh well, I gave up and just used a case statement.

I set a variable like this: SDF2=`date --date="$1" "+%b" | tr '[:lower:]' '[:upper:]'

This gives me the Month and all in upper case.

Then I ran each Month through a case statement
case "$SDF2" in
JAN)
commands
;;
FEB)
commands
;;

and on and on until DEC and then the *) statement.
esac


Top
 Profile  
 PostPosted: Thu May 20, 2010 8:37 am   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 535
this seems to work:
Code:
read -p'Enter Date (DD-MM-YYYY): ' Date
if (( ${#Date} != 10 )) && [[ ! $Date =~ [0-9]{2}-[0-9]{2}-[0-9]{4} ]]
then echo "$Date not valid date"; exit 1
else DD=${Date::2} D=${DD#0}
     MM=${Date:3:2} M=${MM#0}
     YYYY=${Date: -4}
     if ! (( D > 0 && D <= 31 ))
     then echo "$DD not valid day"; exit 1
     else if ! (( M > 0 && M <= 12 ))
          then echo "$MM not valid month"; exit 1
          fi
     fi
     date -d"$YYYY-$MM-$DD" >/dev/null 2>&1 && echo "$Date is valid"
fi


Last edited by Watael on Sun May 23, 2010 8:37 pm, edited 2 times in total.

Top
 Profile  
 PostPosted: Thu May 20, 2010 3:05 pm   
User avatar

Joined: Tue Apr 27, 2010 2:28 pm
Posts: 172
Location: Czech Republic
Watael wrote:
this seems to work

It does not work for dates like 09-09-2010, because 09 is taken as octal number in comparison, which in turn cannot contain 9. If you want to use arithmetics with (()), you have to remove leading zeros from numbers.


Top
 Profile  
 PostPosted: Thu May 20, 2010 4:30 pm   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 535
yes, thanks.

I modified the previous post.


Top
 Profile  
 PostPosted: Sun May 23, 2010 8:00 pm   

Joined: Wed May 19, 2010 9:56 am
Posts: 21
Watael, thanks for that wonderful bit of code. I am putting it to use.

However, it almost drove me mad for a little while because it was not working.

Please make one more minor change for all. There is an "eelse" vice an "else: about 4 lines down.

EDIT ... 24 May 2010 ...
Actually the below code has nothing to do with what Watael showed us. The below was
for capturing the files betweena range of dates. With Watael's code I confirmed the date format which
is one part of my script. With the below I get the files I need between dates.


I also got this method to work with find

After setting up a few variables....
RMPSTORE=/var/www/html/yum/RPMS/64bit/RHEL5.3
STARTTIME=`date --date="$1" "+%Y%m%d0000.01"`
ENDTIME=`date --date="$2" "+%Y%m%d2359.59"`

touch -t $STARTTIME $RPMSTORE/RPMBEGIN
touch -t $ENDTIME $RPMSTORE/RPMEND

find $RPMSTORE -type f -name '*.rpm' -newer "$RPMSTORE/RPMBEGIN" ! -newer "$RPMSTORE/RPMEND" -exec ls -a {} \; -print > /tmp/cd_bur
n_dir/RPMLIST

Also, I appreciate the clear posting of the pattern matching usage. All the examples I saw online showed the curly braces like these { } being delimited with a backslash ( \ ). Your example does not use them. WOW! What a great little tidbit of info. That may have been my problem all along because I was always delimiting them and assuming it was correct.


Last edited by geelsu on Mon May 24, 2010 7:30 am, edited 1 time in total.

Top
 Profile  
 PostPosted: Mon May 24, 2010 7:26 am   

Joined: Wed May 19, 2010 9:56 am
Posts: 21
Watael

When you get a moment ( no rush ) could you explain to me what a line like

MM=${Date:3:2} M=${MM#0}

is doing? I can pick out bits and pieces of what seems to be happening, but I don't fully
understand it.

Thanks.


Top
 Profile  
 PostPosted: Mon May 24, 2010 8:25 am   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 535
from man bash /Parameter Expansion
${parameter:offset:length}
Code:
# here it takes 2 chars from the third char
Date=02-09-2010
MM=${Date:3:2}
echo $MM
09

${parameter#word}
Code:
# here it deletes leading zero
echo ${MM#0}
9


Top
 Profile  
 PostPosted: Mon May 24, 2010 12:40 pm   

Joined: Wed May 19, 2010 9:56 am
Posts: 21
Thank you. Now, I know how Archimedes felt when he understood water displacement, though I won't go running through the streets naked. :)


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 11 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 6 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP