I checked all the previous threads related to this and tried this.
My input is all numbers or decimals greater than zero everytime.
I want to check the same in the korn shell script.
Just validate the string to be numeric.
This is what I am doing.
var="12345"
if [[ "$var" != +([0-9]) -o "$var" != +([0-9]).+([0-9]) ]] ; then
echo "not numric"
else
echo "it is numeric"
fi
I get a syntax error -o unexpected.
-o is used as a logical or operator right ?
Please let me know if am doing something wrong.
The problem with your expression is that the number may contain a + and/or a decimal point or it may not but you arent making that clear to ksh so how about a simpler regular expression like...
var="12345"
if [[ "$var" = *([+-])*([0-9])*(.)*([0-9]) ]]; then
echo "it is numeric"
else
echo "not numeric"
fi
Depends if you can guarantee that 100% of the time...otherwise *(+-) will take care of the absence or presence of a plus/minus sign so ultimately its your choice.
var="12345"
if [[ "$var" = *([+-])*([0-9])*(.)*([0-9]) ]]; then
echo "it is numeric"
else
echo "not numeric"
fi
At the same time I also want to check if var is null. as in the user did not enter any value .The issue is the shell script must continue executing if the parameter is numeric ( integer or decimal ) or null .
How can i check for null value at the same time while i check for numeric values?
I am fairly new to this forum so apologies if this has been posted before. Fairly close to Shamrock's, but I like this numeric check from Bolsky's "The New Kornshell":