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

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Add non-integers using ksh 2dumb Shell Programming and Scripting 18 04-13-2009 01:28 AM
integers in the if statement Deanne Shell Programming and Scripting 5 01-22-2008 05:14 AM
Check for Empty Command Argument Nysif Steve UNIX for Dummies Questions & Answers 6 09-19-2007 04:59 PM
check if argument is an ip address in bash/sh marcpascual Shell Programming and Scripting 3 08-17-2007 04:18 AM
how to check if the argument contain wildcard (*,?) ? gusla UNIX for Dummies Questions & Answers 3 04-02-2002 07:45 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 11-01-2002
elchalateco elchalateco is offline
Registered User
  
 

Join Date: Sep 2002
Posts: 11
Check if argument passed is an integers

How do I check if the argument passed to a script is an integer?

I am writting a script that will take to integers and want to be able to check before I go on.


I am using bourne shell.

Thanks in advance
  #2 (permalink)  
Old 11-01-2002
Vishnu Vishnu is offline
Registered User
  
 

Join Date: Aug 2002
Location: Marlboro, MA
Posts: 114

Code:
echo $string | grep "^[0-9]+$"
echo $?

$? will be 0 only if $string is a sequence of digits and nothing else...
  #3 (permalink)  
Old 11-04-2002
elchalateco elchalateco is offline
Registered User
  
 

Join Date: Sep 2002
Posts: 11
Sorry but that does not work.

I have this script that is working except for negative integers or when someone enters a + infront of the integer.

Why isn't this accepting negative integers or integer with a + sign?


#!/bin/sh
checkinput()
{
case $1 in [0-9]|[0-9]*[0-9][\.]) return 0 ;; esac
return 1
}

exitusage()
{
echo "Usage: Proj2 Integer1 Integer2"
exit
}

[ $# -ne 2 ] && exitusage
checkinput $1 || exitusage && A=$1
checkinput $2 || exitusage && B=$2

Thanks in advance
  #4 (permalink)  
Old 11-04-2002
Vishnu Vishnu is offline
Registered User
  
 

Join Date: Aug 2002
Location: Marlboro, MA
Posts: 114
replace your checkinput() func with this...


Code:

checkinput() 
{ 

echo $1 | grep '^(\+|-)?[0-9]+$'

return $?

}

you may find this helpful...

[\?&]value= A URL parameter value in a URL.

[A-Z]\\[A-Z0-9_]+)+ An uppercase DOS/Windows full path that (a) is not the root of a drive, and (b) has only letters, numbers, and underscores in its text.

[A-Za-z][A-Za-z0-9_]* A ColdFusion variable with no qualifier.

([A-Za-z][A-Za-z0-9_]*)(\.[A-Za-z][A-Za-z0-9_]*)? A ColdFusion variable with no more than one qualifier; for example, Form.VarName, but not Form.Image.VarName.

(\+|-)?[1-9][0-9]* An integer that does not begin with a zero and has an optional sign.

(\+|-)?[1-9][0-9]*(\.[0-9]*)? A real number.

(\+|-)?[1-9]\.[0-9]*E(\+|-)?[0-9]+ A real number in engineering notation.

a{2,4} Two to four occurrences of "a": aa, aaa, aaaa.

(ba){3,} At least three "ba" pairs: bababa, babababa, and so on..


Cheers!
Vishnu.

Last edited by Vishnu; 11-04-2002 at 04:32 PM..
  #5 (permalink)  
Old 11-04-2002
elchalateco elchalateco is offline
Registered User
  
 

Join Date: Sep 2002
Posts: 11
I certanly apreciate all your help. but this:
checkinput()
{

echo $1 | grep '^(\+|-)?[0-9]+$'

return $?

}

doesn't work either. I'll keep tryng.

Thanks a million.
  #6 (permalink)  
Old 11-04-2002
Vishnu Vishnu is offline
Registered User
  
 

Join Date: Aug 2002
Location: Marlboro, MA
Posts: 114
it is always a good idea to test things on command line before putting in a script.. which I hope you are already doing..

you can do few tests echoing the return code...

echo -3243 | grep '^(\+|-)?[0-9]+$'
echo $?

echo -abc | grep '^(\+|-)?[0-9]+$'
echo $?

let me know if these tests work on your unix system...

Cheers!
Vishnu.
  #7 (permalink)  
Old 11-04-2002
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,131
Quote:
Originally posted by Vishnu
checkinput()
{

echo $1 | grep '^(\+|-)?[0-9]+$'

return $?

}
I think you need to use -E with grep to invoke extended regular expressions. But also, if the argument passes the test, it will go to stdout. The -q option will fix that. So try this:
Code:
checkinput() 
{ 

echo $1 | grep -Eq '^(\+|-)?[0-9]+$'

return $?

}

Closed Thread

Bookmarks

Tags
regex, regular expressions

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 12:49 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