The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Advanced & Expert Users
Google UNIX.COM


UNIX for Advanced & Expert Users Advanced UNIX and Linux questions go here. Expert-to-Expert.

More UNIX and Linux Forum Topics You Might Find Helpful
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

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #8  
Old 10-09-2008
Moderator
 

Join Date: Sep 2007
Location: Germany
Posts: 1,031
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
Reply With Quote
  #9  
Old 10-09-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,650
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
Reply With Quote
  #10  
Old 10-09-2008
Moderator
 

Join Date: Sep 2007
Location: Germany
Posts: 1,031
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! (though I never needed that yet). But aren't there tools about for everything?!
Reply With Quote
  #11  
Old 10-10-2008
Moderator
 

Join Date: Dec 2003
Location: /dev/fl
Posts: 1,061
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
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 03:30 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0