I have to make a script that receives two parameters, of which the second must be an integer. I have no idea how to validate that.
if test $2 =~ "^[0-9]+$"
then
echo "\nNumero positivo entero"
else
echo "\nError: El numero $2 no es un numero entero positivo!!!\a"
fi
You can use regular expressions to validate numbers, whether they are integers, negative integers or decimals, you could do something like this:
the regular expressions are:
[1.] ^-? this expression checks the beginning of the string and if it contains any character only one -
[2.] [0-9] checks that they are numbers from 0 to 9
[3.] the + character specifies the or operator
[4.] ([.][0-9]+)$ confirms if the entered value was a decimal number and checks the end of the string with $
The above code receives a keyboard argument and checks if what was entered was really a number, be it positive, negative or decimal.
It can be validated with a
case
:You can make a method that receives parameters, that takes the value of the second and validates if it is Integer or not, for example:
If we execute the method with the following values:
we obtain:
If we execute the method with the following values:
we obtain:
What you can do is filter it by error control seeing that if it is a digit and if it is greater than 0 it is an integer otherwise, well, no.