Operating System - HP-UX
1831645 Members
2369 Online
110029 Solutions
New Discussion

Re: Need to determine is variable is numeric (Scripting)

 
SOLVED
Go to solution
Mike Rightmire
Frequent Advisor

Need to determine is variable is numeric (Scripting)

Hey Gang!

I need a way to determine if a variable is an integer or not. Nothing complicated, just something that would function in the following script ...

#!/bin/sh

read variable1

if test (variable1 is an integer)
then
echo Variable 1 is an integer
else
echo Variable 1 is text or a non-integer number
fi

Obviously the script is going to do more than the echo statements, but you get the idea. The user may enter variable1 as text, a number or anything else the read statement can assign variable1 to be...and the if statement cannot error out.

I am stumped and any help would be greatly appreciate. Any method by which to accomplish the basic jist of this script is fine to.

Thanks for the help!
Mike
"If we treated each person we met as if they were carrying an unspeakable burden, we might almost treat each other as we should." Dale Carnegie
9 REPLIES 9
Sridhar Bhaskarla
Honored Contributor
Solution

Re: Need to determine is variable is numeric (Scripting)

Try this fun

printf "Type in something:"
read i
if [ $i ]
then
echo "${i}0 + 0 "|bc |grep syntax > /dev/null
if [ $? = 0 ]
then
echo You typed some text
else
echo You typed a number
fi

else
echo "You typed nothing"
fi
You may be disappointed if you fail, but you are doomed if you don't try
A. Clay Stephenson
Acclaimed Contributor

Re: Need to determine is variable is numeric (Scripting)

Hi Mike:

Here is one approach using a shell function plus a small test script.

#!/usr/bin/sh

is_an_integer()
{
H=1
if [ $# -ge 1 ]
then
XX=`echo ${1} | awk '/^-*[0-9]+$/ { print $0}'`
if [ -n "${XX}" ]
then
H=0
fi
fi
return ${H}
} # is_an_integer

VARS="78 jack909 -909 90-9 0"

for VAR in ${VARS}
do
echo "${VAR} \c"
is_an_integer ${VAR}
STAT=$?
if [ ${STAT} -eq 0 ]
then
echo "Yes"
else
echo "No"
fi
done

Regards, Clay
If it ain't broke, I can fix that.
Mike Rightmire
Frequent Advisor

Re: Need to determine is variable is numeric (Scripting)

Worked like a charm!!! Thanks. For my edification, how exactly does the logic work....what is happening during the script to determine what is what. Just so I understand better. The script is working GREAT! Thanks!
Mike
"If we treated each person we met as if they were carrying an unspeakable burden, we might almost treat each other as we should." Dale Carnegie
Sridhar Bhaskarla
Honored Contributor

Re: Need to determine is variable is numeric (Scripting)

Mike,

This is not a professional script and is based on a very "raw" logic.

What we are doing here is that we are trying to
add the variable to an integer.

If the variable is a text, obviously the above operation will give a syntax error.

Else, it will be successful.

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
James R. Ferguson
Acclaimed Contributor

Re: Need to determine is variable is numeric (Scripting)

Hi Mike:

This works quite well:

#!/usr/bin/sh
typeset X=$1
if [ `expr "$X" : '[0-9,\-]*'` -ne `expr "$X" : '.*'` ]
then
echo "not a number!"
else
echo "ok, is a number!"
fi
#.end.

Regards!

...JRF...
Curtis Larson_1
Valued Contributor

Re: Need to determine is variable is numeric (Scripting)

case "$number" in
+([0-9])) print "it is a integer";;
*) print "isn't a integer";;
esac
James R. Ferguson
Acclaimed Contributor

Re: Need to determine is variable is numeric (Scripting)

Hi Mike:

BTW, the snipet of code I suggested works by returning and comparing the number of characters in each 'expr'ession. If the number of numeric characters in the first expression consists (and, the way I wrote it, including the minus sign) is equal to the total number of characters as expressed in the second expresssion, then the first expression must consist only of numbers and therefore be "numeric".

Regards!

...JRF...
Wodisch
Honored Contributor

Re: Need to determine is variable is numeric (Scripting)

Hello Mike,

if do not need to use an "if" statement, then
you may be easier with "case":

echo "enter something: \c"
read answer
case "$answer" in
[0-9]*) echo "Yes, it is a number" ;;
*) echo "No, definitely not a number" ;;
esac

and you should always quote substitutions!!!

HTH,
Wodisch
Bill Hassell
Honored Contributor

Re: Need to determine is variable is numeric (Scripting)

There are always many different ways to accomplish the same thing in Unix. Here is yet another way:

if [ $( echo $VARIABLE1 | /usr/bin/tr -d [:digit:]
| /usr/bin/wc -c) -gt 1 ]
then
echo "$VARIABLE1 contains non-digits"
fi
---

The logic:

Test for all numbers by running the string through /usr/bin/tr to remove all digits. If the resultant string length is greater than 1, then non-digits exist. The reason for -gt 1 is that the string terminator is counted,

Note that I use ALL CAPS for env variables to make them easier to distinguish among the shell commands. I always use full pathnames for Unix commands in scripts for security and to avoid issues with messed-up $PATH variables.


Bill Hassell, sysadmin