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 04-27-2009
devtakh devtakh is offline
Registered User
  
 

Join Date: Oct 2007
Location: Bangalore
Posts: 514
try this:

Code:
function validatenumber
{
number="$1"
if [ -z $number ];then
echo "not  something" > &2;return 1
fi

if [ "${number%${number#?}} = "-" ]; then
testvalue="${number#?}"
fi

if [ "${number%${number#?}} = "+" ]; then
testvalue="${number#?}"
fi

nodigits="$(echo $testvalue | sed 's/[[:digit:]]]//g')"
if [ ! -z $nodigits ]; then
echo "Invalid Number" > &2
return 1
fi

return 0
}

if validatenumber "$1"; then
echo "No is a valid integer"
fi

cheers,
Devaraj Takhellambam
  #2 (permalink)  
Old 09-21-2005
bakunin bakunin is offline Forum Staff  
Bughunter Extraordinaire
  
 

Join Date: May 2005
Location: In the leftmost byte of /dev/kmem
Posts: 1,628
The problem can easily be solved by considering what a "number" is actually and since this is a good opportunity to show how to develop regular expressions I will explain that in greater detail:

1st Try
The only characters allowed should be 0-9. This covers for "123", but not for "123.0". Hence

2nd Try
Only 0-9 are allowed, save for exactly 1 character, which may be a fullstop. This definition covers "123" and "456.78", leaves out (quite correctly) "123.456.789", but doesn't cover "-123.0" or "+123.0". Hence

3rd Try
In addition to 2nd Try the first character may be "+" or "-" optionally. This covers every number and leaves out any non-number, so this solution is perfect.

We develop a simple sed-statement from there, by consecutively throwing out all "allowed" chars and look, if something remains. If something remains it is non-numeric, if not, then it is numeric:

Code:
if [ -n "$( print - "$VarInQuestion"           |\
            sed 's/^[+-]//;s/[0-9]//g;s/\.//'   \
          )" ] ; then
     print - "$VarInQuestion is non-numeric"
else
     print - "$VarInQuestion is numeric"
fi
Here is the sed-statement in detail:

s/^[+-]// - see 3rd try, we chop off leading +/- signs
s/[0-9]//g - see 1st try, we throw out all numerical digits
s/\.// - see 2nd try, we throw out one possible decimal point


bakunin
  #3 (permalink)  
Old 09-21-2005
shihabvk shihabvk is offline
Registered User
  
 

Join Date: May 2005
Posts: 54
Hi All..
Thanks a lot....
It is working fine now...
Shihab
  #4 (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 ] {
}
  #5 (permalink)  
Old 04-10-2009
fnds fnds is offline
Registered User
  
 

Join Date: Apr 2009
Posts: 3
Code:
$
$ x=1; if [ "`expr $x - $x 2>/dev/null`" == "0" ]; then echo is number; else echo not number; fi
is number
$ x=-1; if [ "`expr $x - $x 2>/dev/null`" == "0" ]; then echo is number; else echo not number; fi
is number
$ x=a; if [ "`expr $x - $x 2>/dev/null`" == "0" ]; then echo is number; else echo not number; fi
not number
$ x=1a; if [ "`expr $x - $x 2>/dev/null`" == "0" ]; then echo is number; else echo not number; fi
not number
$
  #6 (permalink)  
Old 04-27-2009
fnds fnds is offline
Registered User
  
 

Join Date: Apr 2009
Posts: 3
This solution is better as it works for decimals as well:

Code:
$ x=a; printf "%g" $x >/dev/null 2>&1; if [ $? -eq 0 ]; then echo is number; else echo not number; fi
not number
$ x=9a; printf "%g" $x >/dev/null 2>&1; if [ $? -eq 0 ]; then echo is number; else echo not number; fi
not number
$ x=9; printf "%g" $x >/dev/null 2>&1; if [ $? -eq 0 ]; then echo is number; else echo not number; fi
is number
$ x=9.1; printf "%g" $x >/dev/null 2>&1; if [ $? -eq 0 ]; then echo is number; else echo not number; fi
is number
$ x=-9.1; printf "%g" $x >/dev/null 2>&1; if [ $? -eq 0 ]; then echo is number; else echo not number; fi
is number
$ x=-9; printf "%g" $x >/dev/null 2>&1; if [ $? -eq 0 ]; then echo is number; else echo not number; fi
is number
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 03:38 PM.


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