Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 274
  • Last Modified:

How can I verify script's arguments type?

Dear experts,
I know how to use $# in a script, but I need help to verify that a script's argument is numeric (not string), an interger or floating point?
Thanks
0
kmsadr
Asked:
kmsadr
1 Solution
 
HamdyHassanCommented:
one idea


x=$1

echo x | grep -v "^[0-9]*$" > /dev/null 2>&
if [ "$?" == "0" ]
then
  echo it is number field
else
  echo it is none number field
if

0
 
HamdyHassanCommented:
syntax error.

try this, this is working

x=$1
echo $x | grep "^[0-9]*$" > /dev/null 2>&1
if [ $? -eq 0 ] ; then
 echo it is number field
else
 echo it is none number field
fi


0
 
chris_calabreseCommented:
If in ksh, there's always

typeset -i fubar="$1"

or some such. This will bomb out the script (though you can catch it with a 'trap') if the vbl ever takes on a non-integer value.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
TintinCommented:
Here's a korn shell function (by Brian Hiles) that will check for a valid integer or floating point.

function isnum # number...
{ set -o noglob
 typeset n
 typeset -i retval=0
 for n
 do case $1 in
  (?([-+])+([0-9])?(.)*([0-9])?([Ee]?([-+])+([0-9])))
   ;;
  (?([-+])*([0-9])?(.)+([0-9])?([Ee]?([-+])+([0-9])))
   ;;
  *) let retval+=1 ;;
  esac
 done
 return retval
}
0
 
kmsadrAuthor Commented:
Thanks for your comments. I like both comments from HamdyHassan and Tintin. And I have hard time which comments to accept. I whish I could accept both comments as answer. Since Tintin's comment covered both integer and floating point. I accept his comment. If I can find a way I will give equal points to HamdyHassan as well.
0
 
HamdyHassanCommented:
Thanks kmsadr for your comments.
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now