- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Validating a numeric value
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-23-2003 02:21 PM
11-23-2003 02:21 PM
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.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-23-2003 06:16 PM
11-23-2003 06:16 PM
Solutionso 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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-23-2003 07:28 PM
11-23-2003 07:28 PM
Re: Validating a numeric value
The grep -v solution shown above sounds sensible.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-23-2003 08:17 PM
11-23-2003 08:17 PM
Re: Validating a numeric value
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-23-2003 10:47 PM
11-23-2003 10:47 PM
Re: Validating a numeric value
let Y=X
if test Y -ne X
then
echo integer expected
fi
greetings,
Michael
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-24-2003 12:03 AM
11-24-2003 12:03 AM
Re: Validating a numeric value
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-24-2003 02:11 AM
11-24-2003 02:11 AM
Re: Validating a numeric value
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-24-2003 08:01 AM
11-24-2003 08:01 AM
Re: Validating a numeric value
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.