- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: Need to determine is variable is numeric (Scri...
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
10-05-2001 10:23 AM
10-05-2001 10:23 AM
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-05-2001 10:41 AM
10-05-2001 10:41 AM
Solutionprintf "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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-05-2001 11:01 AM
10-05-2001 11:01 AM
Re: Need to determine is variable is numeric (Scripting)
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-05-2001 11:09 AM
10-05-2001 11:09 AM
Re: Need to determine is variable is numeric (Scripting)
Mike
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-05-2001 11:14 AM
10-05-2001 11:14 AM
Re: Need to determine is variable is numeric (Scripting)
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-05-2001 11:17 AM
10-05-2001 11:17 AM
Re: Need to determine is variable is numeric (Scripting)
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-05-2001 11:55 AM
10-05-2001 11:55 AM
Re: Need to determine is variable is numeric (Scripting)
+([0-9])) print "it is a integer";;
*) print "isn't a integer";;
esac
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-05-2001 07:02 PM
10-05-2001 07:02 PM
Re: Need to determine is variable is numeric (Scripting)
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-06-2001 09:41 AM
10-06-2001 09:41 AM
Re: Need to determine is variable is numeric (Scripting)
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-06-2001 11:52 AM
10-06-2001 11:52 AM
Re: Need to determine is variable is numeric (Scripting)
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