![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| UNIX for Advanced & Expert Users Advanced UNIX and Linux questions go here. Expert-to-Expert. |
|
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Checking if string contains integer | haz | Shell Programming and Scripting | 7 | 09-10-2006 11:51 PM |
| conersting string to integer | abb058 | UNIX for Dummies Questions & Answers | 2 | 08-23-2006 04:52 AM |
| C function to test string or integer | qqq | High Level Programming | 3 | 03-09-2005 10:55 PM |
| Integer to String | psilva | High Level Programming | 2 | 08-17-2001 09:14 AM |
| convert from an integer to a string | mojomonkeyhelper | UNIX for Dummies Questions & Answers | 6 | 03-29-2001 01:15 PM |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#8
|
|||
|
|||
|
I thought of case 1st too, but I was not able to put the expressions together so it would work correctly. Since I was nosy I tried your case script, and it does not work for me either like my former tries with case (just changed $string to $1):
Code:
root@isau02:/data/tmp/testfeld> ./era.ksh ab1cde Pure alpha |
|
#9
|
|||
|
|||
|
You are correct of course; the matching is greedy, and tries its darndest to find a match, so using a negation between wildcards will find a match if there is a way to match it, regardless of the other characters. So the label "pure alpha" is wrong; it should be "alpha + possibly numbers", or the logic should be changed to do additional cases within that case statement.
Code:
case $string in *[!0-9A-Za-z]*) echo "Not pure alpha + numbers" ;; '') echo "Empty string (duh)" ;; *[0-9]*[!0-9]*|*[!0-9]*[0-9]*) echo "Mixed alpha + numbers" ;; *[!0-9]*) echo "Pure alpha";; *) echo "Pure numbers" ;; esac |
|
#10
|
|||
|
|||
|
How greedy!
I guess we found, thanks to the OP, that we are lacking a fine small binary standard tool to check stuff like this! |
|
#11
|
|||
|
|||
|
It is probably time we started using POSIX character classes in our examples more often!
Code:
#!/bin/bash shopt -s extglob INPUT="123" case $INPUT in ( +([[:digit:]]) ) echo "$INPUT is all numbers" ;; ( +([[:alpha:]]) ) echo "$INPUT is all characters" ;; ( +([[:alnum:]]) ) echo "$INPUT is alphanumeric" ;; ( *) echo "$INPUT is empty or something unknown" ;; esac exit 0 |
|||
| Google The UNIX and Linux Forums |