diff options
author | An-Cheng Huang <ancheng@vyatta.com> | 2007-11-12 13:06:02 -0800 |
---|---|---|
committer | An-Cheng Huang <ancheng@vyatta.com> | 2007-11-12 13:06:02 -0800 |
commit | b7fc9e0f6d6105ba2203f219743d4b269415e84b (patch) | |
tree | ef6586dfc62798c2b17487b443864699aca55f31 /examples/functions/isnum.bash | |
download | vyatta-bash-b7fc9e0f6d6105ba2203f219743d4b269415e84b.tar.gz vyatta-bash-b7fc9e0f6d6105ba2203f219743d4b269415e84b.zip |
initial import from bash_3.1dfsg.orig.tar.gz
Diffstat (limited to 'examples/functions/isnum.bash')
-rw-r--r-- | examples/functions/isnum.bash | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/examples/functions/isnum.bash b/examples/functions/isnum.bash new file mode 100644 index 0000000..b733965 --- /dev/null +++ b/examples/functions/isnum.bash @@ -0,0 +1,52 @@ +#From: jrmartin@rainey.blueneptune.com (James R. Martin) +#Newsgroups: comp.unix.shell +#Subject: Re: testing user input on numeric or character value +#Date: 26 Nov 1997 01:28:43 GMT + +# isnum returns True if its argument is a valid number, +# and False (retval=1) if it is any other string. +# The first pattern requires a digit before the decimal +# point, and the second after the decimal point. + +# BASH NOTE: make sure you have executed `shopt -s extglob' before +# trying to use this function, or it will not work + +isnum() # string +{ + case $1 in + ?([-+])+([0-9])?(.)*([0-9])?([Ee]?([-+])+([0-9])) ) + return 0;; + ?([-+])*([0-9])?(.)+([0-9])?([Ee]?([-+])+([0-9])) ) + return 0;; + *) return 1;; + esac +} + +isnum2() # string +{ + case $1 in + ?([-+])+([[:digit:]])?(.)*([[:digit:]])?([Ee]?([-+])+([[:digit:]])) ) + return 0;; + ?([-+])*([[:digit:]])?(.)+([[:digit:]])?([Ee]?([-+])+([[:digit:]])) ) + return 0;; + *) return 1;; + esac +} + +isint() # string +{ + case $1 in + ?([-+])+([0-9]) ) + return 0;; + *) return 1;; + esac +} + +isint2() # string +{ + case $1 in + ?([-+])+([[:digit:]]) ) + return 0;; + *) return 1;; + esac +} |