Computing.Net > Forums > Unix > Check argument Type

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Check argument Type

Reply to Message Icon

Name: Frank Cappuchio
Date: November 4, 2002 at 12:08:32 Pacific
OS: Uwin for windows
CPU/Ram: P II 128 MG
Comment:

How do I find out if the arguments passed to a script are of the integer type. Not just one argument. It can be at least 3

Frank Cappuchio



Sponsored Link
Ads by Google

Response Number 1
Name: James Boothe
Date: November 4, 2002 at 15:25:08 Pacific
Reply:

I like to use expr for the numeric test. The easiest way to loop through your parameters is to shift them down, but of course that makes them disappear. In the following script, I first save the parameter list, then check them all, then set them back up with the set command.

#!/bin/sh

if [ $# -eq 0 ] ; then
echo 'Need at least one parameter'
exit 1
fi

allparms=$*
k=0

until [ $# -eq 0 ]
do
if expr "$1" : "[0-9]*$" > /dev/null ; then
echo "$1 (all digits)"
else
echo "$1 (NOT all digits)"
((k=k+1))
fi
shift
done

set $allparms

if [ $k -gt 0 ] ; then
echo "\nAt least one parameter was not all digits\n"
fi

exit 0


0

Response Number 2
Name: Frank Cappuchio
Date: November 5, 2002 at 05:16:47 Pacific
Reply:

James,

This is exactly what I want, now I need to be able to enter numbers such as:

-5 8
8 -10
0 0
-1 -4

how can I modidy the script to accept this type of patterns. No other pattern will be valid.

Thanks a million.
Frank


0

Response Number 3
Name: James Boothe
Date: November 5, 2002 at 08:02:44 Pacific
Reply:

The following tests for the first character being either a dash or digit, to be followed by zero or more digits:

if expr "X$1" : "X[-|0-9][0-9]*$" > /dev/null ; then
echo "$1 (valid)"
else
echo "$1 (NOT valid)"
((k=k+1))
fi

The X avoids confusion when the argument is a single character that would be taken as an operator such as dash or equal-sign (see man page).

A problem with the above is that it would show a single dash as valid. To insist that a dash be followed by at least one digit, we can use a compound expression:

if expr X$1 : "X-[0-9][0-9]*$" \
\| X$1 : "X[0-9]*$" > /dev/null ; then
echo "$1 (valid)"
else
echo "$1 (NOT valid)"
((k=k+1))
fi

Now a single dash would not be a match, (although -0 would match).


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More


Unix Printing using ftp in Korn Shell



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: Check argument Type

CSH Runtime Arguments www.computing.net/answers/unix/csh-runtime-arguments/3825.html

Unix cmd for find and remove www.computing.net/answers/unix/unix-cmd-for-find-and-remove/2376.html

Check that the argument is a file www.computing.net/answers/unix/check-that-the-argument-is-a-file/6890.html