Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Check for Intigers

Status
Not open for further replies.

ojosVerdes

Technical User
Oct 10, 2002
50
US
How do I check if the argument passed to a script is an interger?

I am using bourne shell.

Thanks in advance
 
Hi:

Check this thread:

thread822-382650

The thread is on functions, but there's 3 different ways used to check whether an argument is integer. Once should work for you.

Regards,

Ed
 
a simple way

case x$1 in x) #nothing entered
;; x[0-9]*[0-9]) #integer entered
;; *) #something else entered
;; esac -----------
when they don't ask you anymore, where they are come from, and they don't tell you anymore, where they go ... you'r getting older !
 
Jamisar,
This is the code I have and I embedded you code in but is not working as expected.

What Am I doing wrong?

Thanks in advance.


#!/bin/sh
num=$#
[ $# -ne 2 ] && { echo "Usage: $0 Integer1 Integer2"; exit;}
x=0
while test $x -lt $num
x=`expr $x + 1`
do
case x$x in x)#nothing entered
;; x[0-9]*[0-9]) echo Integer $#
;; *) echo Something Else.
;; esac
done
 
i don't see why :(
try a workaround.... why so complex???


checkinput()
{
case $1 in [0-9]|[0-9]*[0-9]) return 0 ;; esac
return 1
}

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

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

-----------
when they don't ask you anymore, where they are come from, and they don't tell you anymore, where they go ... you'r getting older !
 
Jamisar,
Thanks for taking your time. I am a novice user and I was not really aware of fuctions. perhaps I will find out more about them.

Yes your way works good except when you enter 2.5 or 5.2. It lets those numbers go through. I only want integers and they can be like -56 or just plain 45. positive integers dont need a sign.

Thanks again.

Ojos Verdes
 
ojos verdes:
i fergot negativ
prepend a [\-] in the case statement
nota 5.3 is NOT an integer :( -----------
when they don't ask you anymore, where they are come from, and they don't tell you anymore, where they go ... you'r getting older !
 
Jamisar,
I want negative integers. for example the following should be valid:
Proj2 -5 2 #valid
Proj2 2 -8 #Valid
Invalids
Proj2 -5 .2 # invalid because the .2
proj2 1.5 6 #invalid because the 1.5

the part that is not working is the negative integers.
Thanks again
 
sure you can write a c, perl or awk program, this just a shell ...

1.sed purge '-0'
2 sed purge '+'
3.sed purge .[0-9]*
this is fast, no computing just regexp.
NOTA the input 'qq77mm' is not checked for.
i personnaly prefer /bin/[bd]c.

checkinput()
{
eval $1=`/bin/echo $2|/bin/sed -e 's/-0/0/;s/+//' -ne 's/\([\-]\)*\([0-9][0-9]*\).*/\1\2/p'`
}

checkinput A $1 ; [ x$A = x ] && exitusage
checkinput B $2 ; [ x$B = x ] && exitusage -----------
when they don't ask you anymore, where they are come from, and they don't tell you anymore, where they go ... you'r getting older !
 
this is the only practicable way without writing c, perl, awk and other

checkinput()
{
eval $1=`/bin/echo "a=$2;a*=10;scale=0;a/=10;a;quit" | /bin/bc 2>&1 | /bin/grep error >/dev/null` && return 1
return 0
}

checkinput A $1 || exitusage
checkinput B $2 || exitusage

...compute... in $A is the 1.param in $B the second. -----------
when they don't ask you anymore, where they are come from, and they don't tell you anymore, where they go ... you'r getting older !
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top