Regex in Linux does not work
Hi All,
Here's the script I was testing. In Linux my shell enviroment is bash. The objective is to test if my input is a whole number like 1, or 52 or 1000 and running the script it will not say anything as expected. Otherwise for any other input that doesn't match the criteria it will say "error: Not a number" and quit. #!/bin/bash re='^[0-9]+$' printf "`echo -n Enter a number or anuthing to test:` \n" read char if ! [[ "$char" =~ "$re" ]]; then echo "error: Not a number !!!" >&2; exit 1 fi FYI the above script works fine in one Linux pc in a bash shell. But the same script when used in another Linux pc which uses Bourne shell (sh) - it does not work :( All the whole numbers and everything else it gives the error message. Can somebody please help shed some light ? thanks, frodobag |
What distro is on that other PC?
|
Ubuntu
|
Quote:
In particular, I think the [[ ... ]] construct doesn't exist in the Bourne shell. What is the error message? EDIT: Your program doesn't work because of the following (from the bash reference guide): Quote:
If char has a value of, say, 'fdg^[0-9]+$lk', the expression [[ $char =~ "$re" ]] will be true. Thus, to check if char is a number, remove the quotes around $re. You can also remove the quotes around $char, since they are not needed inside [[ ... ]]. |
no error message. Just that after I run the script and when I type in 1, 52, or 1000 it erroneously outputs "error: Not a number !!" instead of outputting nothing and quietly exiting as would be expected.
|
Quote:
|
Nope that didn't work. But oddly enough I was trying some variations and with re='^[0-9]+$' and now it works! But thanks all, at least it jogs my thoughts a bit.
|
A side note: I am using the above test script for a more complex script to read a log file. Now since when I type in whole numbers on the keyboard , I guess the regex recognise it as actually numbers so it is correct. But when I extract a value with my more complex script from the log file using grep , cut ,sed, that value , although I see it as a number but is it possible the regex comparison I use above, "sees" it as a text ? and maybe thats why it says "not a number" ?
|
I don't use Bourne shell, but the bash abs guide notes that both the [[ ... ]] extended test and regex match aren't supported in Bourne and are portability issues.
|
Yeah, the original sh shell is less capable than bash (hence the name ;) ).
I'd stick with the latter, unless of course you want to move up to eg Perl (which is red hot on regexes...) |
Quote:
Your bash fragment above says "not a number" because when your $re is surrounded by quotes, you match $char against a mere string, not a regular expression. Since 12345 doesn't contain the string ^[0-9]+$, the test fails. When you remove the quotes, $re is interpreted as a regexp and the test succeeds. You say "nope it doesn't work", but I wonder what it is that doesn't work? I am curious to see your code, your input and your output. Edit: I participate here in parts because I learn. I didn't know about these details of =~ and would like to gain an even deeper understanding. I don't insist for insistance's sake. |
looks like you need to use:
Code:
# instead of "$re" |
There's a couple of good explanations/HOWTOs here
http://www.tldp.org/LDP/abs/html/regexp.html http://www.itworld.com/article/26933...pressions.html - this one has an example of your problem :) |
Thanks guys for the hints.
Here's a snippet of the other script: #!/bin/bash #re='^\d(\d)?(\d)?(\d)?(\d)?$ ' re='^[0-9]+$' char=` ...just grepping some whole numbers from a log file here, like 1234 or 56, etc...` if ! [[ $char =~ $re ]] ; then echo "error: Not a number !!!" else echo " Whole number - good" fi I've tried... if ! [[ "$char" =~ $re ]] ; then ...as well...along with other regex but the output was "error: not a number" even when the char value was something like 1234, when I expect it to say " Whole number - good" instead. Only the char value is let's say a text like THISTEXT or with special characters like 1234-456:7:8 then it should say "error: Not a number". But as of now all these 3 examples it says " error" ,which at this point still doesn't work. I probably need to find an alternative to the =~ operator and [[..]] |
I think I should elaborate char=` ...just grepping some whole numbers from a log file here, like 1234 or 56, etc...`
The way I use to grep might be the problem. I use... tac logfile | grep "(1.)" |grep -E '[0-9]{1,4}' | head -1 So the line in the logfile gets selected for example.... (1.) This is a line 1234 and that's it. Date: 12-12-2015 So it will pick out 1234 correctly but still, with the extra grep -E command, it doesn't help. While the result of grep of 1234 is correct, the result of the comparison operator +~ is not, which always say "error:Not a number". |
All times are GMT -5. The time now is 05:24 AM. |