Shell Script To Check For a Valid Floating-Point Value - GeeksforGeeks

20 Apr, 2021
User Input can be hectic to manage especially if it’s about numbers. Let’s say you wanted to perform mathematical calculations in your app or script. It’s not hard to say that it will have to deal with floating-point numbers. It’s easy in many statically typed programming languages but if it has to do in terminals then that can get quite annoying as shell scripting or bash doesn’t have any data types.
We have to deal without data types and make use of programming skills to actually make it work. Hopefully, we have regular expressions in Bash to deal with such problems. Regular Expressions or RegEx, in short, is an expression as the name suggests that can find certain patterns in a text field. It quite a powerful tool to have in a Programmer’s toolkit. So, let’s see how to solve the problem.
So let’s find a modular approach to the problem statement. We need to check whether the input variable is a decimal (floating-point) number or not. To do that we must have only digits in the variable and just a single decimal point. The decimal point can be anywhere, but it has to be only one. To check if only digits are present we can use Regular  Expression and further we can also check for a decimal point from the second to the second-last character as a decimal point cannot be in the beginning and at the end. We can simply use conditional statements to verify these necessary conditions. 
Firstly we will take the user input and store it in any preferable named variable. 
We are using read to prompt the user for input. Along with the “read” command we will be using -p as an argument to the base “read” command for a prompt that displays the text such as enter the number or any preferable relevant information.
Now we need to check that the input is a number or not. This allows us to be sure for the next condition to be a floating-point value otherwise it will conflict with numbers and floating-point numbers.
Regular Expression allows us to find a pattern in the text or a variable. Here we are using n as an input variable. We use =~ to make bash aware that the coming expression is a RegEx. After that, we use ^ and $ for checking the pattern from start to end.
A number can have positive or negative signs so does a floating value and hence it is applied to both conditions. Anything between [ ] has to match with the input, in this case, we include 0-9 as any number can be in the input. We have added a “?” after the +- signs to make those optional i.e. it’s not mandatory to include +  to indicate the numbers as positive. An * sign to repeat the previous expression zero times or more times as to include more digits furthermore. After a number is being identified we use “+” to include the following conditions to the preceding once or more times. The “\” in the floating-point condition is just behaving to turn as an ordinary character. We finally add “.’ between “\” and “?” to merge the expressions and to make decimal optional, but it won’t call a number as the condition is already satisfied by the previous if condition. Hence, we end the expression with the “$” sign and print the satisfied condition of the input being an integer, floating-point value, or any expression.
#!/bin/bash

# User Input
read -p "Enter the number : " n


# Check for a number
if [[ $n =~ ^[+-]?[0-9]+$ ]];then
echo "The Input is an integer."


# Check for a floating number
elif [[ $n =~ ^[+-]?[0-9]*\.?[0-9]+$ ]];then
echo "Input is a float "
else
echo "The input is not a number."
fi
Output:
ShellScript Output with many test-cases
By making a Shell Script using Regular Expression we verified a user input to be a floating-point number or not. The script doesn’t consider scientific notation as a floating-point value that also can be achieved but to keep it simple for simplistic use this is working accurately. 
Picked
Shell Script
Linux-Unix
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Thread functions in C/C++
nohup Command in Linux with Examples
mv command in Linux with examples
scp command in Linux with Examples
Docker - COPY Instruction
chown command in Linux with Examples
nslookup command in Linux with Examples
SED command in Linux | Set 2
Named Pipe or FIFO with example C program
uniq Command in LINUX with examples