Unix/Linux Go Back    


Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here.

How to check whether a string is number or not

Shell Programming and Scripting


Tags
regex, regular expressions

Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 09-21-2005
shihabvk shihabvk is offline
Registered User
 
Join Date: May 2005
Last Activity: 14 July 2009, 8:56 AM EDT
Posts: 54
Thanks: 0
Thanked 0 Times in 0 Posts
How to check whether a string is number or not

Hi ,
I am facing a problem .. which looks simple... but took 2 days of mine.. even now it is not solved completely..

I have one variable..., want to know whether that variable contains number... canbe +ve or -ve ...

Values
+35 --- number
-43 --- number
45A -- non number
34+45 -- non number

please help me
Shihab
Sponsored Links
    #2  
Old Unix and Linux 09-21-2005
vino's Unix or Linux Image
vino vino is offline Forum Advisor  
Supporter (in vino veritas)
 
Join Date: Feb 2005
Last Activity: 12 March 2015, 5:33 AM EDT
Location: Sydney, Down Under
Posts: 2,848
Thanks: 0
Thanked 13 Times in 13 Posts
Look at the solution given in this post

Let me knows if it falters for any condition.

Vino

Last edited by vino; 09-21-2005 at 05:42 AM..
Sponsored Links
    #3  
Old Unix and Linux 09-21-2005
RishiPahuja's Unix or Linux Image
RishiPahuja RishiPahuja is offline
Registered User
 
Join Date: Apr 2005
Last Activity: 15 April 2011, 1:11 AM EDT
Location: Bangalore, India
Posts: 203
Thanks: 0
Thanked 0 Times in 0 Posts
MySQL

Another solution vaild only for integers, decimals not allowed Linux

#!/bin/ksh
echo "Enter val : \c"
read num
expr $num + 1 2> /dev/null
if [ $? = 0 ]
then
echo "Val was numeric"
else
echo "Val was non-numeric"
fi

rishi Linux

Last edited by RishiPahuja; 09-21-2005 at 05:58 AM..
    #4  
Old Unix and Linux 09-21-2005
shihabvk shihabvk is offline
Registered User
 
Join Date: May 2005
Last Activity: 14 July 2009, 8:56 AM EDT
Posts: 54
Thanks: 0
Thanked 0 Times in 0 Posts
Hi rishi ,
It won't work if the number is like +567
example expr +567 + 1 .. will fail


Regards
Shihab
Sponsored Links
    #5  
Old Unix and Linux 09-21-2005
sanjustudy sanjustudy is offline
Registered User
 
Join Date: Aug 2005
Last Activity: 21 November 2007, 6:22 AM EST
Location: India
Posts: 17
Thanks: 0
Thanked 1 Time in 1 Post
number or not

if u r using tcl or perl there is finction calles isnumber

if [ isnumber $a ] {
}
Linux
Sponsored Links
    #6  
Old Unix and Linux 09-21-2005
RishiPahuja's Unix or Linux Image
RishiPahuja RishiPahuja is offline
Registered User
 
Join Date: Apr 2005
Last Activity: 15 April 2011, 1:11 AM EDT
Location: Bangalore, India
Posts: 203
Thanks: 0
Thanked 0 Times in 0 Posts
MySQL

Quote:
Originally Posted by shihabvk
Hi rishi ,
It won't work if the number is like +567
example expr +567 + 1 .. will fail


Regards
Shihab

A small edit Shihab,

#!/bin/ksh
echo "Enter val : \c"
read num
expr "$num + 1" 2> /dev/null
if [ $? = 0 ]
then
echo "Val was numeric"
else
echo "Val was non-numeric"
fi

Note the double qutoes around the expression.

Rishi
Sponsored Links
    #7  
Old Unix and Linux 09-21-2005
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
 
Join Date: Mar 2005
Last Activity: 16 July 2015, 8:07 AM EDT
Location: classification algos
Posts: 3,206
Thanks: 19
Thanked 30 Times in 27 Posts
how abt this,


Code:
[[ `echo $num | sed 's/^[-+0-9][0-9]*//' | wc -c` -eq 1 ]] && echo "itz number" && exit 0
echo "itz not number"

not thoroughly tested
plz let me know, if it faints somewhere
Sponsored Links
Closed Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Check parameter is number or string Eclecticaa Shell Programming and Scripting 2 06-11-2011 03:02 PM
changing number in bash (number is in form of string) Learnerabc Shell Programming and Scripting 3 11-02-2010 05:53 PM
How to Check given string is number in shell script? balajiora Shell Programming and Scripting 6 06-12-2009 09:06 AM
how to search string and number in one file and check in the other file knshree Shell Programming and Scripting 9 08-24-2007 04:29 AM
check whether variable number or string rolex.mp UNIX for Dummies Questions & Answers 4 02-21-2007 11:12 PM



All times are GMT -4. The time now is 10:22 AM.