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
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
echo $string | grep "^[0-9]+$"
echo $?
$? will be 0 only if $string is a sequence of digits and nothing else...
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...
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.
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.
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.
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:
checkinput()
{
echo $1 | grep -Eq '^(\+|-)?[0-9]+$'
return $?
}
They do not work.They returned 1.
Thanks again.
Ooops!! Thank you, Perderabo for correcting my typo!!...
grep -E should work for elchalateco, he should have tried it on the command line...
Cheers!
Vishnu.
Vishnu,
YOU'R THE MAN!
Thanks a million
--------------------------------------------------------------------------------checkinput()
{
echo $1 | grep -Eq '^(\+|-)?[0-9]+$'
return $?
}
The above function does not allow the use of + sign in front of a number. even thoug positive numbers don't need the + sing to indicate that they are positives. I need this for this function.
I have tryed couple of alternatives with no luck.
Thanks
That was a wrong claim... try this on command line...
echo +2345 | grep -E '^(\+|-)?[0-9]+$'
I hope you understood what '^(\+|-)?[0-9]+$' means...
Since you seem concerned with selective and intelligent matches, I would suggest you to go through some good book on regular expressions...
here is one worth it's bucks...
Cheers!
Vishnu.
Although I think its great passing around all these regexes, I thought I might break it down anyway, it might help someone. Please correct me if I am wrong.
'^(\+|-)?[0-9]+$' :
^ = start of the line
(\+|-) = either positive or a negative
? = preceeding character, zero or once; therefore either + or - occurs or it doesn't
[0-9] =any numeric character
+ = preceeding character, once or more times therefore [0-9]+ is any number between 0-9 more than once
$ = end of line
I want to thank every one who contributed .
Guys I offer an apology. The scritp does not work on my box at home; however it run perfectly at work.
Thanks again.
Frank