The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
google unix.com



UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
[csh] checking for specific character ranges in a variable userix Shell Programming and Scripting 5 05-11-2008 07:56 AM
Ksh Checking if string has 2 characters and does not contain digits? developncode UNIX for Dummies Questions & Answers 1 04-08-2008 03:19 PM
Eliminate variable checking in a script BMetelsky Shell Programming and Scripting 8 12-04-2007 10:59 AM
checking valid values in variable MizzGail Shell Programming and Scripting 3 10-23-2006 03:20 PM
checking a variable is within range Poison Ivy Shell Programming and Scripting 2 08-17-2006 05:33 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 04-08-2008
developncode developncode is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 14
ksh Checking if variable has 5 digits

How could I check if a numeric variable has 5 digits in KSH...I have a zipcode variable that I know will always be 5 digits, and I want to print out an error if it is less or more than 5 digits the problem is that I have it as:
if [[ $zipcode -gt 99999 || $zipcode -lt 00001 ]]
but this won't work because the statement doesn't see 0001 as being less than 00001...pls help.
  #2 (permalink)  
Old 04-08-2008
DukeNuke2's Avatar
DukeNuke2 DukeNuke2 is online now Forum Staff  
Soulman
  
 

Join Date: Jul 2006
Location: Germany, Berlin
Posts: 2,951
you are looking for "wc -m". this counts the characters in an input.

for example:

Code:
$ echo 11111 | wc -m
   6
its always +1 for the return character!
  #3 (permalink)  
Old 04-08-2008
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,293
DukeNuke2,

You misread the question, he searches for exact 5 digits. Try this:

Code:
grep "[^0-9][0-9]\{5\}[^0-9]"
Regards
  #4 (permalink)  
Old 04-08-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
Or even

Code:
case $var in ?????) continue;; *) yikes; oops; er um --whoa;; esac
Change ? to [0-9] to check that all characters are numerical, while you're at it.

I hope you know that if your audience is international, "ZIP code" is not very universal.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 11:34 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0