![]() |
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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 |
|
||||
|
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 |
|
||||
|
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.. |
|
||||
|
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. |
![]() |
| Bookmarks |
| Tags |
| regex, regular expressions |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|