The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
awk: How do i check to see if a variable is a number? natdeamer Shell Programming and Scripting 1 09-06-2007 07:31 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
read string, check string length and cut ozzy80 Shell Programming and Scripting 9 03-21-2007 05:56 PM
check whether variable number or string rolex.mp UNIX for Dummies Questions & Answers 4 02-22-2007 12:12 AM
Check if variable is a number handak9 UNIX for Dummies Questions & Answers 2 03-01-2005 08:27 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rating: Thread Rating: 1 votes, 5.00 average. Display Modes
  #1 (permalink)  
Old 09-21-2005
shihabvk shihabvk is offline
Registered User
  
 

Join Date: May 2005
Posts: 54
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
  #2 (permalink)  
Old 09-21-2005
vino's Avatar
vino vino is offline Forum Staff  
Supporter (in vino veritas)
  
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,796
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..
  #3 (permalink)  
Old 09-21-2005
RishiPahuja's Avatar
RishiPahuja RishiPahuja is offline
Registered User
  
 

Join Date: Apr 2005
Location: Bangalore, India
Posts: 203
Thumbs up

Another solution vaild only for integers, decimals not allowed

#!/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

Last edited by RishiPahuja; 09-21-2005 at 05:58 AM..
  #4 (permalink)  
Old 09-21-2005
shihabvk shihabvk is offline
Registered User
  
 

Join Date: May 2005
Posts: 54
Hi rishi ,
It won't work if the number is like +567
example expr +567 + 1 .. will fail


Regards
Shihab
  #5 (permalink)  
Old 09-21-2005
sanjustudy sanjustudy is offline
Registered User
  
 

Join Date: Aug 2005
Location: India
Posts: 17
number or not

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

if [ isnumber $a ] {
}
  #6 (permalink)  
Old 09-21-2005
RishiPahuja's Avatar
RishiPahuja RishiPahuja is offline
Registered User
  
 

Join Date: Apr 2005
Location: Bangalore, India
Posts: 203
Thumbs up

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
  #7 (permalink)  
Old 09-21-2005
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
  
 

Join Date: Mar 2005
Location: leaf node in B+ tree
Posts: 2,944
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 Thread

Bookmarks

Tags
regex, regular expressions

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 04:32 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0