![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread |
Rating:
|
Display Modes |
|
|
|
||||
|
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 |
|
||||
|
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
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 |
|
||||
|
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 $ |
|
||||
|
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 |
![]() |
| Bookmarks |
| Tags |
| regex, regular expressions |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|