| Line | |
|---|
| 1 | #From: [email protected] (James R. Martin)
|
|---|
| 2 | #Newsgroups: comp.unix.shell
|
|---|
| 3 | #Subject: Re: testing user input on numeric or character value
|
|---|
| 4 | #Date: 26 Nov 1997 01:28:43 GMT
|
|---|
| 5 |
|
|---|
| 6 | # isnum returns True if its argument is a valid number,
|
|---|
| 7 | # and False (retval=1) if it is any other string.
|
|---|
| 8 | # The first pattern requires a digit before the decimal
|
|---|
| 9 | # point, and the second after the decimal point.
|
|---|
| 10 |
|
|---|
| 11 | # BASH NOTE: make sure you have executed `shopt -s extglob' before
|
|---|
| 12 | # trying to use this function, or it will not work
|
|---|
| 13 |
|
|---|
| 14 | isnum() # string
|
|---|
| 15 | {
|
|---|
| 16 | case $1 in
|
|---|
| 17 | ?([-+])+([0-9])?(.)*([0-9])?([Ee]?([-+])+([0-9])) )
|
|---|
| 18 | return 0;;
|
|---|
| 19 | ?([-+])*([0-9])?(.)+([0-9])?([Ee]?([-+])+([0-9])) )
|
|---|
| 20 | return 0;;
|
|---|
| 21 | *) return 1;;
|
|---|
| 22 | esac
|
|---|
| 23 | }
|
|---|
| 24 |
|
|---|
| 25 | isnum2() # string
|
|---|
| 26 | {
|
|---|
| 27 | case $1 in
|
|---|
| 28 | ?([-+])+([[:digit:]])?(.)*([[:digit:]])?([Ee]?([-+])+([[:digit:]])) )
|
|---|
| 29 | return 0;;
|
|---|
| 30 | ?([-+])*([[:digit:]])?(.)+([[:digit:]])?([Ee]?([-+])+([[:digit:]])) )
|
|---|
| 31 | return 0;;
|
|---|
| 32 | *) return 1;;
|
|---|
| 33 | esac
|
|---|
| 34 | }
|
|---|
| 35 |
|
|---|
| 36 | isint() # string
|
|---|
| 37 | {
|
|---|
| 38 | case $1 in
|
|---|
| 39 | ?([-+])+([0-9]) )
|
|---|
| 40 | return 0;;
|
|---|
| 41 | *) return 1;;
|
|---|
| 42 | esac
|
|---|
| 43 | }
|
|---|
| 44 |
|
|---|
| 45 | isint2() # string
|
|---|
| 46 | {
|
|---|
| 47 | case $1 in
|
|---|
| 48 | ?([-+])+([[:digit:]]) )
|
|---|
| 49 | return 0;;
|
|---|
| 50 | *) return 1;;
|
|---|
| 51 | esac
|
|---|
| 52 | }
|
|---|
Note:
See
TracBrowser
for help on using the repository browser.