I've been using the following regex below in a bash script on RHEL 5.5 using version
GNU bash, version 3.2.25(1)-release
I've tried using the script on RHEL 6.3 which uses GNU bash, version 4.1.2(1)-release
I assume there's been alot of changes to bash since that's quite a jump in revisions. I'm wondering if anyone can help me sort what might have to be modified in the code to make this work again.
If I enter an e-mail address in a simple format like:
user.user@test.com the scripts sees this as an invalid e-mail now, whereas before this would pass the regex.
#!/bin/bash
echo Enter the users e-mail address.
read ADDRESS
if [[ "$ADDRESS" =~ "^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$" ]] ; then
echo $?
echo Proceeding!!
else
echo $?
echo You entered an invalid e-mail address!!!
exit 1
fi
You also shouldn't depend on the meaning of $?, after the test (if [ ...]).
$ cat SomeScript
ls sdadadsasdasd
echo $?
ls sdadadsasdasd
if [ 1 -eq 1 ]; then
echo $?
echo Yes, 1 is 1
else
echo $?
echo No, not 1 is not 1
fi
$ ./SomeScript
ls: sdadadsasdasd: No such file or directory
1
ls: sdadadsasdasd: No such file or directory
0
Yes, 1 is 1
I'm having the same issue in this regex. if I remove the quotes in this one then
something like Test Name comes back as invalid whereas in bash 3.2 Test Name would be valid but test name would be invalid. The idea is the first and last name need to start with an upper case letter of course.
if [[ "$givenName" =~ "^[A-Z]([a-z]|'[A-Za-z])+\s+[A-Z]([a-z]|'[A-Za-z])+(-[A-Z]([a-z]|'[A-Za-z])+)*$" ]]
then
echo -e "${bldgrn}$givenName is a valid name.${txtrst}"
else
echo -e "${bldred}$givenName is an invalid name!!${txtrst}"
exit 1
fi
At some point between version 3 and version 4, bash began to treat a quoted right-hand operand of =~ as a string instead of a pattern. This explains why it used to work one way and now it works another.
If I use the code below without the double and single quotes Test Name is invalid.
if [[ "$givenName" =~ ^[A-Z]([a-z]|[A-Za-z])+\s+[A-Z]([a-z]|[A-Za-z])+(-[A-Z]([a-z]|[A-Za-z])+)*$ ]]
then
echo -e "${bldgrn}$givenName is a valid name.${txtrst}"
else
echo -e "${bldred}$givenName is an invalid name!!${txtrst}"
exit 1
fi
This is perfect. It actually works better that it did before because now a hyphenated last name is now valid as well.
Thank you.
I guess I'm going to have to do some more reading on regular expressions so I can understand them better. Do you have any recommendations on a resource?