check if a string is numeric

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.

|| is a boolean or

How about this thread?

$ var=123A24
$ echo $var | grep -q "^[0-9]*$" && echo "OK" || echo "Not OK"
Not OK
$ var=12345
$ echo $var | grep -q "^[0-9]*$" && echo "OK" || echo "Not OK"
OK

Since you have the Korn shell you can do this:

[ ! -z "$VAR" -a -z "${VAR/[0-9]*/}" ] && echo "$VAR is all-numeric"

I think the OP wanted to cover the floats as well as the ints.

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
1 Like

awesome !!!! That worked.

But when I know that every time the user will input only positive numbers should i still include + or - in code ?

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.

try this out
name=+1234

echo $name | awk '/[+|-][[:digit:]]|[[:digit:]]/  {print -1}'

Hi shamrock

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":

#!/bin/ksh
isnum()
{
case $1 in
( ?([-+])+([0-9])?(.)*([0-9])?([Ee]?([-+])+([0-9])) )
return 0;;
( ?([-+])*([0-9])?(.)+([0-9])?([Ee]?([-+])+([0-9])) )
return 0;;
*) return 1;;
esac
}
myvar="-23.45"
isnum $myvar && echo "numeric" || echo "NOT numeric"
 

Then all you have to do is separate the numeric regular expression from the one that is null by a vertical bar...

if  [[ "$var" = ?(""|*([+-])*([0-9])*(.)*([0-9])) ]]; then
1 Like

Here is a simpler alternative if you are using ksh93:

declare -F foo
if (foo=$var) 2>/dev/null; then
    echo "it is numeric"
else
    echo "it is not numeric"
fi

Hi Shamrock

Thank you so much .That worked.
And in the code I modified it to this.

if  [[ "$var" != ?(""|*([+-])*([0-9])*(.)*([0-9])) ]]; then
 
 
If var != null or var!=numeric ,

only then my shell will contiue. otherwise it will exit the process.

Can you tell me about the " ? "that you used in the code?