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
Another newb question: how to use test for zero-length string ? lumix Shell Programming and Scripting 4 12-15-2007 05:43 PM
ksh - test if string contains alphanumeric... tugger Shell Programming and Scripting 3 10-16-2007 04:23 AM
test if a filename exists with specified string (ksh) bob122480 Shell Programming and Scripting 6 01-21-2007 03:47 PM
C function to test string or integer qqq High Level Programming 3 03-10-2005 01:55 AM
string test? hedrict UNIX for Dummies Questions & Answers 9 03-29-2002 03:57 PM

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

Join Date: Feb 2008
Posts: 8
test a string...

Hi! I'm using

echo $string | grep "[^0-9]" -c

to test in a script if a string is a number and it seems to work. But how can i find, for example, if a string is a four figures number ?

Thanks to all!
  #2 (permalink)  
Old 02-12-2008
vino's Avatar
vino vino is offline Forum Staff  
Supporter (in vino veritas)
  
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,798
Quote:
Originally Posted by Kaminski View Post
Hi! I'm using

echo $string | grep "[^0-9]" -c

to test in a script if a string is a number and it seems to work. But how can i find, for example, if a string is a four figures number ?

Thanks to all!
How does that command of yours test if the string is a number ? What it does is, it gives you a count of the number of non-numeric patterns found.

This will test a 4 digit string

Code:
echo $string | egrep "^[0-9]{4}$"

Last edited by vino; 02-12-2008 at 05:44 AM..
  #3 (permalink)  
Old 02-12-2008
plelie2 plelie2 is offline
Registered User
  
 

Join Date: Jul 2002
Location: Belgium
Posts: 27
Wink

what is the goal ?
must your number be min 4 digits ?
must your number be min and max 4 digits ?

there are several options
I prefer the following

subtract 999 from your number and check the result to see if this is bigger than 0 => this way you know your number is at leaset 1000

subtract 9999 from your number and check the result to see if this is lower than 1 => this wat you know your number is max 9999

vb:

[dummy920]@@ecar0o:/home/oracle> t=8
[dummy920]@@ecar0o:/home/oracle> ((a=$t-999))
[dummy920]@@ecar0o:/home/oracle> if (( $a > 0 ))
more>then
more>echo "min 4 digits"
more>else
more>echo "too small"
more>fi
too small
[dummy920]@@ecar0o:/home/oracle> t=10000
[dummy920]@@ecar0o:/home/oracle> ((a=$t-9999))
[dummy920]@@ecar0o:/home/oracle> if (( $a < 1 ))
more>then
more>echo "max 4 digits"
more>else
more>echo "too big"
more>fi
too big
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 06:17 PM.


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