The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
ksh pattern matching ripat Shell Programming and Scripting 5 02-10-2008 01:44 PM
help need for pattern matching HIMANI UNIX for Dummies Questions & Answers 10 01-22-2008 04:30 AM
pattern matching mercuryshipzz Shell Programming and Scripting 4 01-14-2008 08:01 PM
pattern matching malle Shell Programming and Scripting 3 01-31-2007 02:23 AM
Pattern matching sed leemjesse Shell Programming and Scripting 3 03-23-2005 01:06 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 12-14-2007
Registered User
 

Join Date: Dec 2007
Posts: 15
pattern matching in an if-then

I'm having trouble getting my syntax right here; I want to test the value of $1 and return true if it is between 0 and 9. I've tried many combinations including

if [ $1 -eq [0-9] ]

but none have worked when passing, say, "5" into $1.

Any help is appreciated. Thanks.
Reply With Quote
Forum Sponsor
  #2  
Old 12-14-2007
Moderator
 

Join Date: Feb 2007
Posts: 2,329
Code:
if [ "$1" -gt 0 ] && [ "$1" -lt 10 ]
or

Code:
if (("$1" > 0)) && (("$1" < 10))
Regards
Reply With Quote
  #3  
Old 12-14-2007
Registered User
 

Join Date: Dec 2007
Posts: 15
Thanks for the reply. I think I may have done a lousy job of articulating my objective though. I'm looking to test for the presence of a non-numeric character; the above will produce an error. Perhaps that's the only way to do this, i.e. test for an error in a numeric operation?

Thanks again.
Reply With Quote
  #4  
Old 12-14-2007
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 4,298
One way but it just says there exists a non-numeric:
Code:
#!/bin/ksh
check=$( echo "1234x6789" | tr -dc '[:alpha:]' )
if [[ ${#check} -gt 0 ]] ; then
  echo 'found non-numeric'
else
   echo 'all numbers'
fi
You can also grep:
Code:
$(echo "1234x6789" | grep -q '[A-Z,a-z]') && echo 'found it'
Reply With Quote
  #5  
Old 12-14-2007
Read Only
 

Join Date: Nov 2007
Posts: 165
Try:

Code:
case "$1" in
  [0-9]) echo "It's a digit!";;
  *) echo "Non-digit.";;
esac
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 07:25 AM.


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