?
Solved

How can I verify script's arguments type?

Posted on 2003-03-28
6
Medium Priority
?
273 Views
Last Modified: 2010-04-21
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
Comment
Question by:kmsadr
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 9

Expert Comment

by:HamdyHassan
ID: 8227541
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
 
LVL 9

Expert Comment

by:HamdyHassan
ID: 8227580
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
 
LVL 14

Expert Comment

by:chris_calabrese
ID: 8228362
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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
LVL 48

Accepted Solution

by:
Tintin earned 300 total points
ID: 8231994
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
 

Author Comment

by:kmsadr
ID: 8236326
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
 
LVL 9

Expert Comment

by:HamdyHassan
ID: 8238660
Thanks kmsadr for your comments.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In tuning file systems on the Solaris Operating System, changing some parameters of a file system usually destroys the data on it. For instance, changing the cache segment block size in the volume of a T3 requires that you delete the existing volu…
I have been running these systems for a few years now and I am just very happy with them.   I just wanted to share the manual that I have created for upgrades and other things.  Oooh yes! FreeBSD makes me happy (as a server), no maintenance and I al…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question