Register
It is currently Thu Nov 27, 2014 5:46 am

function to see if variable is numeric


All times are UTC - 6 hours


Post new topic Reply to topic  [ 8 posts ] 
Author Message
 PostPosted: Mon May 16, 2005 8:36 pm   
Site Admin
User avatar

Joined: Sun May 15, 2005 9:36 pm
Posts: 673
Location: Des Moines, Iowa
Originally posted by jbsnake

Code:
### check to see if variable is numeric ###
#                                         #
#                                         #
#                                         #
### call like #############################
# if isNumeric $var                       #
# then                                    #
#    echo "numeric"                    #
# else                                    #
#    echo "non-numeric"                #
# fi                                      #
###########################################
function isNumeric()
{
Numeric='y'
len=`echo ${#1}`
for ((i=0; i<$len; i++))
do
   val=`echo ${1:$i:1}`
   echo $val | grep ^[0-9] > /dev/null
   retval=$?
   if [[ $retval -eq 1 ]]
   then
      if [[ $i -eq 0 && $val = '-' ]]
      then
         num="negative"
      else
         Numeric='n'
         break
      fi
   fi
done
if [[ $Numeric = 'y' ]]
then
   return 0
else
   return 1
fi
}
### end isNumeric ###
[/b]


Top
 Profile WWW  
 PostPosted: Wed Nov 15, 2006 8:05 pm   

Joined: Wed Nov 15, 2006 7:56 pm
Posts: 5
A shorter version:

Code:
function isNumeric()
{
   non=${1//[0-9]/}
   return ${#non}
}


Top
 Profile  
 PostPosted: Wed Nov 15, 2006 10:55 pm   
Site Admin

Joined: Tue May 17, 2005 7:31 pm
Posts: 251
Location: Georgia
out of curiosity... what shell are you using for that? i can't seem to make it work


Top
 Profile  
 PostPosted: Thu Nov 16, 2006 7:43 am   

Joined: Wed Nov 15, 2006 7:56 pm
Posts: 5
Code:
$ bash --version
GNU bash, version 3.1.17(1)-release (i586-suse-linux)
Copyright (C) 2005 Free Software Foundation, Inc.


I was surprised to notice I am using Bash 3. Last time I looked it was Bash 2. I have only recently learned about the substitution feature of Shell Parameter Expansion, so maybe it is new.


Top
 Profile  
 PostPosted: Thu Nov 16, 2006 12:29 pm   
Site Admin

Joined: Tue May 17, 2005 7:31 pm
Posts: 251
Location: Georgia
Code:
<- jb@kung:/mnt/jb/jbsnake ->
<- 13:34:15 -> bash --version
GNU bash, version 3.1.17(1)-release (i686-pc-linux-gnu)
Copyright (C) 2005 Free Software Foundation, Inc.


though maybe not for everyone... almost seems like a suse feature
worth looking into i think :wink:


Top
 Profile  
 PostPosted: Thu Nov 16, 2006 1:48 pm   

Joined: Wed Nov 15, 2006 7:56 pm
Posts: 5
You are using the same version of Bash and it does not work!? That is just not right.

Here is a short test script:

Code:
$ cat isnum
#!/bin/bash

function isNumeric()
{
   non=${1//[0-9]/}
   return ${#non}
}


if isNumeric $1
then
  echo it is a number
fi
$ isnum 123
it is a number
$ isnum abc
$


The ${VAR//PATTERN/STRING} is substitution during parameter expansion. VAR is the variable name, // does substitution for each occurrence of PATTERN, and STRING is what is substituted. The PATTERN uses wildcards for filename expansion, not, unfortunately, regular expressions.

This is documented in the info page for Bash and http://tille.xalasys.com/training/bash/ ... t_10_03_03 at the bottom of the page.


Top
 Profile  
 PostPosted: Thu Nov 16, 2006 2:39 pm   
Site Admin
User avatar

Joined: Sun May 15, 2005 9:36 pm
Posts: 673
Location: Des Moines, Iowa
Code:
[~/scripts]
[15:41:41 crouse]$ vi isanum.sh
[~/scripts]
[15:42:07 crouse]$ ./isanum.sh 45g5
[~/scripts]
[15:42:10 crouse]$ ./isanum.sh 1234
it is a number
[~/scripts]


hmmmm worked for me.....


Top
 Profile WWW  
 PostPosted: Thu Nov 16, 2006 9:20 pm   

Joined: Wed Nov 15, 2006 7:56 pm
Posts: 5
Oh good, I was worried!

According to O'Reilly's Learning the Bash Shell page 96, this feature was introduced in version 2.0


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 8 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: steveT and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP