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
Test variable javeed7 Shell Programming and Scripting 2 04-19-2008 07:59 PM
to check variable if its non numeric sachin.gangadha Shell Programming and Scripting 3 12-06-2007 05:33 PM
Perl code to differentiate numeric and non-numeric input Raynon Shell Programming and Scripting 11 08-04-2007 10:32 AM
How to test if a variable is in the right format dbrundrett Shell Programming and Scripting 2 07-05-2004 07:05 AM
how to set a variable to accept alpha-numeric characters? bcaunt Shell Programming and Scripting 3 06-04-2002 03:42 PM

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 Rating: Thread Rating: 1 votes, 5.00 average. Display Modes
  #1 (permalink)  
Old 04-09-2008
developncode developncode is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 14
Question Ksh How to test if variable is numeric??

I'm trying to test and see whether a variable that is being extracted from a line in a file is numeric or not. I have tried everything that I can think of and I cannot figure out how to get it to work. I am trying to test and see if the string extracted contains 5 numeric digits. This is what I have so far:

case $zipcode in
+([0-9])|+([0-9])|+([0-9])|+([0-9])|+([0-9]))
if [[ $zipcode -lt 1 ]] && [[ $zipcode -gt 99999 ]] || [[ $zipcount -ne 6 ]]
then
echo ${myline}
echo "ZIPCODE WARNING! -> ${zipcode}"
fi
;;
*)
echo ${myline}
echo "ZIPCODE WARNING! -> ${zipcode}"
;;
esac
  #2 (permalink)  
Old 04-09-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
Code:
case $zipcode in [0-9][0-9][0-9][0-9][0-9]) ;;
    *) echo "$myline"; echo "zipcode warning: $zipcode" >&2;;
esac
If it's five digits then it can't be bigger than 99999 or less than 00000, so that check seems superfluous. Also you seem to have a logic error there (it cannot be less than one and bigger than 99999). The check for bigger than 0 still needs to be done but it complicates things a bit so I didn't want to dilute this example with the handling of that.

Last edited by era; 04-09-2008 at 03:11 PM.. Reason: make it more similar to original
  #3 (permalink)  
Old 04-09-2008
developncode developncode is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 14
Sorry it was a little tedious, but I just tried it your way and it still doesnt work...it prints out zipcode warnings for every line in the file. even the ones that are legit...I'm not sure what we are doing wrong, but is there another way to do it?
  #4 (permalink)  
Old 04-09-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
Do you have spaces around the digits? Is there a trailing newline?
  #5 (permalink)  
Old 04-09-2008
developncode developncode is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 14
No there aren't any leading or trailing whitespaces or newlines in this variable. I'm not sure why it is giving me warnings for legit zipcodes with 5 digits, but is there another way to check and see if a variable is numeric?
  #6 (permalink)  
Old 04-09-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
There are ways of course, but it would be better IMHO to understand what's wrong here. Does it pass strict numerical test? Does it pass five-character test?

Code:
case $zipcode in *[!0-9]*) echo did not pass all-number test >&2;; esac
case $zipcode in ?????) echo did not pass five-character test >&2;; esac
  #7 (permalink)  
Old 04-09-2008
developncode developncode is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 14
I was able to get it to work...sorry it had one leading whitespace that was keeping it from recognizing that the variable was numeric and not a string. Thanks for all your help I really appreciate it.
Sponsored Links
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 03:30 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