Operating System - HP-UX
1828482 Members
2517 Online
109978 Solutions
New Discussion

Re: Validating a numeric value

 
SOLVED
Go to solution
Paul Verheyen
Occasional Contributor

Validating a numeric value

Hi,

I'm developing a script (using Bourne shell) to reorganise space allocation for Informix database tables. The script displays the original space size (an integer value) and then requests a new value to use.

I'd like to validate this entered value to ensure that no alpha or alternate characters are used such as commas or decimal points. I only want an integer to be accepted. ie. 2000 is okay, but not 19.9 or 35,000.

In post http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=109402

JRF suggests the following.
--------------------
Here's one way, where "X" contains the data of interest:

if [ `expr $X : '[0-9]*'` -ne `expr $X : '.*'` ]
then
echo "Integer expected!"
fi
--------------------------------

I tried this out and got errors. (Syntax error at line 7 : `&' is not expected) I'm guessing this relates to the fact that I'm using Bourne shell? I was pretty lost with the "[" etc too... Haven't encountered these before.

Would the above suggested syntax work in Bourne shell for what I'm trying to do, with only minor changes, or do I need to take a totally different approach?

I thought that maybe testing the result of a mathematical "mod" expression might be a way of achieving things. Any resultant of a mod other than 0 would mean the number isn't an integer. I'm not sure if the function exists though...

Thanks for any assistance.

Regards.

Paul.
I just got lost in thought. It was unfamiliar territory.
7 REPLIES 7
Thierry Poels_1
Honored Contributor
Solution

Re: Validating a numeric value

Hi,

so basically you only want numbers to be entered:

read i

echo $i | grep -q "^[0-9]*$"
if [ $? -eq 0 ]
then
echo integer
else
echo not integer
fi

regards,
Thierry.
All unix flavours are exactly the same . . . . . . . . . . for end users anyway.
Alan Turner
Regular Advisor

Re: Validating a numeric value

The "[" is html speak for a character represented by its ASCII code, in this case, decimal 91, or open square bracket. Similarly, "]" is close square bracket.

The grep -v solution shown above sounds sensible.
Graham Cameron_1
Honored Contributor

Re: Validating a numeric value

Paul

Any reason why you are using Bourne shell, it's pretty archaic?

If you used ksh, or posix-sh, it does all the work for you.
You can declare variables and predefine them as numeric, using typeset -i.

eg

typeset -i number=0
read number && do_something

etc

-- Graham
Computers make it easier to do a lot of things, but most of the things they make it easier to do don't need to be done.
Michael Schulte zur Sur
Honored Contributor

Re: Validating a numeric value

Hi,

let Y=X
if test Y -ne X
then
echo integer expected
fi

greetings,

Michael
john korterman
Honored Contributor

Re: Validating a numeric value

Hi,
tricky, if you also have to prohibit an empty string or a series of spaces. Try for instance this:

#!/usr/bin/sh
EMPTY1=$(echo "$1" | tr -d "[:space:]")
if [ "$EMPTY1" != "" ]
then
EMPTY2=$(echo "$EMPTY1" | tr -d "0-9")
if [ "$EMPTY2" = "" ]
then
echo "$1" is an integer
else
echo "$1" is not an integer
fi
else
echo "$1" is an empty string
fi

or wait for the perl one-liner...

regards,
John K.
it would be nice if you always got a second chance
John Palmer
Honored Contributor

Re: Validating a numeric value

If you're using /usr/bin/sh on an HP system then it's not bourne but the posix shell. The real bourne shell is /usr/old/bin/sh.

In the Posix/korn shell, I think the most logical is to use an if statement of the form:-

if [[ ${X} = +([0-9]) ]];

matches any integer including leading zeroes or:

if [[ ${X} = [1-9]*([0-9]) ]];

matches any integer NOT starting with a zero.

Graham's solution will also work but you'd probably want to include 2>/dev/null on the read statement to avoid the annoying error message 'The specified number is not valid for this command.'.

Remember that recent shell patches have imposed the (in my opinion) unreasonable assumption that unless you declare it otherwise, a number which starts with a 0 is octal rather than decimal. So declare your variable thus:
typeset -i10 X

Then any assignment can be checked by the normal methods such as:-
let X=? 2>/dev/null ||
read X 2>/dev/null
if [[ ${?} -ne 0 ]];
then

Regards,
John
Paul Verheyen
Occasional Contributor

Re: Validating a numeric value

Thank you all for your help.

Thierry's solution will basically suits my needs. The extra information provided by John K and John P will be useful too.

Apologies. I got my shell environments mixed up. I'm a bit of a dill. If you saw a picture of me you'd quickly work that out. :-)

Again, thanks all.

Paul.
I just got lost in thought. It was unfamiliar territory.