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 > 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 and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Ksh How to test if variable is numeric?? developncode UNIX for Dummies Questions & Answers 8 03-19-2009 06:42 PM
Numeric Validation sumesh.abraham Shell Programming and Scripting 12 08-04-2008 11:10 AM
Perl code to differentiate numeric and non-numeric input Raynon Shell Programming and Scripting 11 08-04-2007 11:32 AM
How To Add Numeric Data of A file krishna_sicsr Shell Programming and Scripting 3 03-17-2007 12:51 PM
non-numeric argument TiredOrangeCat UNIX for Dummies Questions & Answers 3 02-13-2007 02:37 AM

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 05-25-2007
malaymaru malaymaru is offline
Registered User
  
 

Join Date: May 2005
Posts: 54
Checking numeric value

Hi,

How can I check numeric value in shell?

I am passing one parameter (integer) and I want to restrict any other characters except 0-9.

./script xyz 10

Here 2nd parameter (10) should be integer only.

Can anyone help on this?

Malay
  #2 (permalink)  
Old 05-25-2007
funksen funksen is offline Forum Advisor  
Registered User
  
 

Join Date: Nov 2006
Location: Austria/Vienna
Posts: 430
Code:
if [[ ! -z $(echo $2 | sed 's/[0-9]//g') ]]
then
echo "integer only"; exit
fi
should work
  #3 (permalink)  
Old 05-25-2007
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,538
Code:
#echo "1234"  | awk '/^[0-9]+$/' 
1234
#echo "12sasd34"  | awk '/^[0-9]+$/'
#
  #4 (permalink)  
Old 05-25-2007
Abhishek Ghose Abhishek Ghose is offline
Registered User
  
 

Join Date: Sep 2005
Location: Chennai
Posts: 81
Oneliner

Here's an oneliner:

awk -v x=$a 'END{if(x==x+0) print "integer"}' /dev/null



In this example , a is shell variable.
If a=10, then the output of above is "integer"
If a="10abhishek", then the output of above is ""
  #5 (permalink)  
Old 05-25-2007
mahesh. mahesh. is offline
Registered User
  
 

Join Date: May 2007
Posts: 4
Hello Iam new user , Can help me,

I want get system open file table value , Can u help.

Thanking you,
Mahesh
  #6 (permalink)  
Old 05-25-2007
aigles's Avatar
aigles aigles is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,423
With bash (extglob option must be on) and ksh :
Code:
if [[ "$var" != +([0-9]) ]]
then
   echo "Not an Integer"
fi

Jean-Pierre.
  #7 (permalink)  
Old 05-25-2007
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,361
Quote:
Originally Posted by malaymaru
How can I check numeric value in shell?

I am passing one parameter (integer) and I want to restrict any other characters except 0-9.

./script xyz 10

Here 2nd parameter (10) should be integer only.

Code:
case $2 in
  *[^0-9]*) echo not integer; exit 1 ;;
esac
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 10:03 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