aboutsummaryrefslogtreecommitdiffstats
path: root/examples/functions/isnum.bash
diff options
context:
space:
mode:
authorJari Aalto <jari.aalto@cante.net>1998-04-17 19:52:44 +0000
committerJari Aalto <jari.aalto@cante.net>2009-09-12 16:46:51 +0000
commitcce855bc5b117cb7ae70064131120687bc69fac0 (patch)
tree39c7a4ec8f6d22ef03df74f2684e6a04fef10399 /examples/functions/isnum.bash
parente8ce775db824de329b81293b4e5d8fbd65624528 (diff)
downloadandroid_external_bash-cce855bc5b117cb7ae70064131120687bc69fac0.tar.gz
android_external_bash-cce855bc5b117cb7ae70064131120687bc69fac0.tar.bz2
android_external_bash-cce855bc5b117cb7ae70064131120687bc69fac0.zip
Imported from ../bash-2.02.tar.gz.
Diffstat (limited to 'examples/functions/isnum.bash')
-rw-r--r--examples/functions/isnum.bash23
1 files changed, 23 insertions, 0 deletions
diff --git a/examples/functions/isnum.bash b/examples/functions/isnum.bash
new file mode 100644
index 0000000..1eff13f
--- /dev/null
+++ b/examples/functions/isnum.bash
@@ -0,0 +1,23 @@
+#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
+
+function 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
+}