Bash RegEx for checking floating point numbers from user input

I am relatively new to bash programming and am currently creating a simple calculator.

He needs to use floating point numbers and verify that they are.

I have a checkNumbers function:

function checkNumber { regExp=[0-9] if [ $testNo =~ $regExp ] then echo "That is a number!" let check=1 else echo "Damn! Not A Number!" fi } 

where I get the user to enter a number like this:

 while [ $check -eq 0] do echo "Please enter a number read testNo echo "" checkNumber done 

This does not work, I get this error:

./calculator.sh: line 39: [: = ~: expected binary operator

line 39:

 if [ $testNo =~ $regExp ] 

I tried a lot of regular expressions like:

 ^*[0-9]*$ 

and

 ^*[0-9]\.[0.9]*$ 

etc.

In addition, I have linked other ways to perform validation:

 case $testNo in ^*[0-9]*$) echo "That is a number!";; *) echo "Error! Not a number!";; esac 

and

 if [ $testNo = ^*[0-9]*$ ] then echo "etc etc" else echo "oops" fi 

I also need this to work with floating point numbers.

Can someone please let me know how I say this?

+6
source share
3 answers

This regular expression ^[-+]?[0-9]+\.?[0-9]*$ will only match digits with optional . :

 $ echo 30 | grep -Eq '^[-+]?[0-9]+\.?[0-9]*$' && echo Match Match $ echo 30.10 | grep -Eq '^[-+]?[0-9]+\.?[0-9]*$' && echo Match Match $ echo 30. | grep -Eq '^[-+]?[0-9]+\.?[0-9]*$' && echo Match Match $ echo +30 | grep -Eq '^[-+]?[0-9]+\.?[0-9]*$' && echo Match Match $ echo -30 | grep -Eq '^[-+]?[0-9]+\.?[0-9]*$' && echo Match Match 

I think when you tried ^*[0-9] , you wanted ^[0-9]*

Rexeplanation:

 ^ # Match start of string [-+]? # Match a leading + or - (optional) [0-9]+ # Match one or more digit \.? # Match a literal . (optional, escaped) [0-9]* # Match zero or more digits $ # Match the end of the string 

Note: this corresponds to the numbers followed . as 30. , not sure if this is acceptable to you.

Edit: do not quote regex

 testNo=30.00 if [[ $testNo =~ ^[+-]?[0-9]+\.?[0-9]*$ ]]; then echo Match fi >>> Match 
+7
source

To use this type of function, you need a conditional expression version [[ ... ]] . [ is the "old" test command and does not process regular expressions at all.

 #! /bin/bash function checkNumber { regExp='^[+-]?([0-9]+\.?|[0-9]*\.[0-9]+)$' if [[ $testNo =~ $regExp ]] then echo "That is a number!" let check=1 else echo "Damn! Not A Number!" fi } testNo=1 checkNumber testNo=-1.2 checkNumber testNo=+.2 checkNumber testNo=+0. checkNumber testNo=a checkNumber testNo=hello2you checkNumber $ ./t.sh That is a number! That is a number! That is a number! That is a number! Damn! Not A Number! Damn! Not A Number! 

See What is the difference between the test, [and [[? .

Regular expression explanation:

 ^ Anchor at start of string $ Anchor at end of string 

These two make regular expressions match the entire string passed; partial matches are not allowed.

 [+-] 

matches either + or - .

 [+-]? 

makes this part optional, so the above matches exactly + , - or nothing at all.

Then there is an alternation (part1|part2) that will match if part1 or part2 part1 .

Part one:

 [0-9]+\.? 

which corresponds to one or more ( + ) digits (but not zero digits / empty sets) and optional . . This processes numbers of the form 123 and 534. .. But not only that . .

The second part of:

 [0-9]*\.[0-9]+ 

This corresponds to zero or more ( * ) digits, and then . followed by one or more digits. This corresponds to all other floats, such as 1.3 or .543 (without exponential notation), but still excludes only . .

+2
source
 #!/bin/bash #script to validate while [ true ]; do clear echo "Introduce the numeric value: " read -r var if [[ $var =~ ^[+-]?([0-9]+)$ ]] then echo "You introduced an integer value (without decimals)" read else if [[ $var =~ ^[+-]?([0-9]+\.)$ ]] then echo "Incomplete floating value (no values provided at the right of the point)" read else if [[ $var =~ ^[+-]?(\.[0-9]+)$ ]] then echo "Incomplete floating value (no values provided at the left of the point)" read else if [[ $var =~ ^[+-]?([0-9]+\.[0-9]+)$ ]] then echo "You introduced a correct floating value" read else echo "You introduced something other than a valid numeric value" read fi fi fi fi done 
+1
source

All Articles