Validate User Input is 16bit signed integer

Discussion of programming on Linux, including shell scripting, perl, python, c/c++, mono, java. Whatever tickles your fancy.
Post Reply
mudasir
Captain
Posts: 565
Joined: Tue Oct 17, 2006 5:23 am
Location: Dubai
Contact:

Validate User Input is 16bit signed integer

Post by mudasir »

Assalam-o-alaikum,

I am making a shell script in which i need user to input a Signed 16bit Integer as arguments to the script.
The script will take 6 arguments which all should be Signed 16bit integer.

I would like to know how can i verify that the value that user has input is a Signed 16bit integer. I need to verify $1 $2 $3 $4 $5 $6.

Looking for a positive and quick reply.
Kind Regards
Mudasir Mirza (RHCE)
(+971)55-1045754
http://www.crystalnetworks.org
http://www.diglinux.com
LinuxFreaK
Site Admin
Posts: 5132
Joined: Fri May 02, 2003 10:24 am
Location: Karachi
Contact:

Re:

Post by LinuxFreaK »

Dear mudasir,
Hello,

You can use something like this ...

Code: Select all

if [ $# -ne 16 ]
then
    echo 1>&2 Usage: $0
    exit 127
fi
Best Regards.
Farrukh Ahmed
lambda
Major General
Posts: 3452
Joined: Tue May 27, 2003 7:04 pm
Location: Lahore
Contact:

Post by lambda »

just check if each number is between -32768 and 32767.
Watch out for the Manners Taliban!
Isn't it amazing how so many people can type "linuxpakistan.net" into their browsers but not "google.com"?
mudasir
Captain
Posts: 565
Joined: Tue Oct 17, 2006 5:23 am
Location: Dubai
Contact:

Post by mudasir »

Assalam-o-alaikum,

Thanks allot for the help, i have completed the user input validation function. Now i am stuck in another mathematical error. Please help me out in this.

my script takes 6 parameters as input
./<script name> X1 Y1 Z1 X2 Y2 Z3

then script perform some calculation on these values, where i am stuck is that, if Y1 value or any other value has a "-" sign with it, shell gives me an error

DIV=`expr $Y2 - $Y1`
T=`expr -$Y1 / $DIV` &> /dev/null

this is where i get an error, error is
"expr: non-numeric argument"

when script run in debug mode i see
++ expr 2 - -2
+ DIV=4
++ expr --2 / 4

the double "--" sign is creating problem.

The formula through which i have to do calculation has a "-" sign so how to overcome this issue.

Thanks in advance.
Kind Regards
Mudasir Mirza (RHCE)
(+971)55-1045754
http://www.crystalnetworks.org
http://www.diglinux.com
lambda
Major General
Posts: 3452
Joined: Tue May 27, 2003 7:04 pm
Location: Lahore
Contact:

Post by lambda »

why don't you explain the mathematics behind the calculation? what does it mean for y1 to be negative in the mathematical equation?
Watch out for the Manners Taliban!
Isn't it amazing how so many people can type "linuxpakistan.net" into their browsers but not "google.com"?
azfar
Captain
Posts: 598
Joined: Tue Mar 23, 2004 1:16 am
Location: Karachi
Contact:

Post by azfar »

you are trying to minus a negative integer from another integer and then divide it by another integer?

if so then use the + operator for subtraction operation too, it will give you the right answer.
Azfar Hashmi
Email : azfarhashmi@hotmail.com
Post Reply