- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- checking if a variable is an integer or a string
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
Discussions
Discussions
Discussions
Forums
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
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-16-2005 07:08 AM
тАО10-16-2005 07:08 AM
does anyone know how can I check whether a varible value contains an integer or a string?
on simple bourne shell script
Itai
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-16-2005 07:19 AM
тАО10-16-2005 07:19 AM
Solutionecho "${MYVAR}" | grep -E -q -e '^-{0,1}[0-9]+$'
GSTAT=${?}
if [ ${GSTAT} -eq 0 ]
then
echo "${MYVAR} is an integer"
else
echo "${MYVAR} ain't an integer"
fi
You can drop the -{0,1} if only positive values pass the test.
- Tags:
- integer
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-16-2005 07:22 AM
тАО10-16-2005 07:22 AM
Re: checking if a variable is an integer or a string
There are a lot of examples inside the ITRC, check below link.
http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=66578
search I used in google "site:forums1.itrc.hp.com integer string"
Best regards,
Robert-Jan
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-16-2005 02:02 PM
тАО10-16-2005 02:02 PM
Re: checking if a variable is an integer or a string
if [ "$(echo $MYVAR | tr -d '[:digit:]')" = "" ]
then
echo "All digits"
else
echo "$MYVAR has non-digits"
fi
Bill Hassell, sysadmin
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-16-2005 10:05 PM
тАО10-16-2005 10:05 PM
Re: checking if a variable is an integer or a string
Say your variable is called $VAR.
typeset -i INT="$VAR" 2> /dev/null
if [ "$?" -eq 0 ]
then
echo "Is an integer"
else
echo "Is not an integer"
fi
- Tags:
- typeset
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-17-2005 12:27 AM
тАО10-17-2005 12:27 AM
Re: checking if a variable is an integer or a string
you really helped my a lot