- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Minor shell script question
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
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
тАО03-08-2007 06:24 PM
тАО03-08-2007 06:24 PM
I don't know how to write the statement in order to distinguish the input is integer. it seems as below
if [ $1 is an integer ]; then
echo "it is an intger"
else
echo "it is a string"
fi
Thanks
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-08-2007 07:01 PM
тАО03-08-2007 07:01 PM
SolutionI think you have to be a little creative to achieve that. This example just deletes all numbers in $1 and if anything still remains, $1 was not an integer:
#!/usr/bin/sh
REST=$(echo "$1"|tr -d [0-9])
if [ "$REST" = "" ]; then
echo "it is an intger"
else
echo "it is a string"
fi
regards,
John K.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-08-2007 07:22 PM
тАО03-08-2007 07:22 PM
Re: Minor shell script question
please see earlier thread:
http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=945981
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-09-2007 12:01 AM
тАО03-09-2007 12:01 AM
Re: Minor shell script question
This handles the determination of unsigned integers:
# N=123
# [ `expr "${N}" : '[0-9]*'` -eq `expr "${N}" : '.*'` ] && echo "Integer" || echo "non-integer"
If the value of 'N' consists only of the digits 0-9 for its whole length, then 'N' is an integer.
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-09-2007 02:26 AM
тАО03-09-2007 02:26 AM
Re: Minor shell script question
sub is_signed_int
{
typeset -i GSTAT=255
if [[ ${#} -eq 1 ]]
then
echo "${1}" | grep -E -q '^[+-]{0,1}[0-9]+$'
GSTAT=${?}
else
echo "Function is_signed_int expects exactly 1 arg" >&2
fi
return ${GSTAT}
} # is_signed_int
sub is_int
{
typeset -i GSTAT=255
if [[ ${#} -eq 1 ]]
then
echo "${1}" | grep -E -q '^[0-9]+$'
GSTAT=${?}
else
echo "Function is_int expects exactly 1 arg" >&2
fi
return ${GSTAT}
} # is_int
typeset -i STAT=0
while [[ ${#} -ge 1 ]]
do
echo "Value ${1} \c"
is_signed_int "${1}"
STAT=${?}
if [[ ${STAT} -eq 0 ]]
then
echo "is a signed int \c"
fi
is_int "${1}"
if [[ ${STAT} -eq 0 ]]
then
echo "is an int\c"
fi
echo
shift
done
The -q option of egrep returns an exit code of 0 if the pattern matches the regular expression and non-zero otherwise.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-11-2007 02:10 PM
тАО03-11-2007 02:10 PM
Re: Minor shell script question
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-11-2007 02:10 PM
тАО03-11-2007 02:10 PM