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.

Validate User Input is 16bit signed integer

Postby mudasir » Thu Oct 01, 2009 2:12 am

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
mudasir
Captain
 
Posts: 565
Joined: Tue Oct 17, 2006 5:23 am
Website: http://www.crystalnetworks.org
Location: Dubai

Re:

Postby LinuxFreaK » Thu Oct 01, 2009 8:30 am

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
LinuxFreaK
Site Admin
 
Posts: 5132
Joined: Fri May 02, 2003 10:24 am
ICQ: 82075802
Website: http://www.linuxpakistan.net/wiki/index.php?pagename=LinuxFreak
WLM: f4fahmed@hotmail.com
Yahoo Messenger: f4fahmed@yahoo.com
AOL: linuxpakistan@aol.com
Location: Karachi

Postby lambda » Thu Oct 01, 2009 9:33 am

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"?
lambda
Major General
 
Posts: 3452
Joined: Tue May 27, 2003 7:04 pm
Website: http://www.hungry.com/~fn/
Location: Lahore

Postby mudasir » Thu Oct 29, 2009 8:37 am

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
mudasir
Captain
 
Posts: 565
Joined: Tue Oct 17, 2006 5:23 am
Website: http://www.crystalnetworks.org
Location: Dubai

Postby lambda » Thu Oct 29, 2009 11:20 am

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"?
lambda
Major General
 
Posts: 3452
Joined: Tue May 27, 2003 7:04 pm
Website: http://www.hungry.com/~fn/
Location: Lahore

Postby azfar » Sat Oct 31, 2009 3:36 pm

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
azfar
Captain
 
Posts: 598
Joined: Tue Mar 23, 2004 1:16 am
WLM: azfarhashmi@hotmail.com
Yahoo Messenger: azfarhusain@yahoo.com
Location: Karachi


Return to “%s” Programming

Who is online

Users browsing this forum: No registered users and 0 guests

cron