Verify a command line argument as NUMERIC

Verify a command line argument as NUMERIC

Post by tya.. » Tue, 22 Feb 2000 04:00:00



Can anyone help me with a general shell scripting question?  I need to be able
to verify an argument passed via command line and ensure that the value is
numeric.  Is there any way to do this?

Thanks

 
 
 

Verify a command line argument as NUMERIC

Post by Jon Hal » Tue, 22 Feb 2000 04:00:00



> Can anyone help me with a general shell scripting question?  I need to be
able
> to verify an argument passed via command line and ensure that the value is
> numeric.  Is there any way to do this?

> Thanks


I think something like

if [ X$(echo $SOMEVAR|tr -d "[0-9]")  = X ]
then
    echo "\$SOMEVAR is numeric or undefined"
else
    echo "\$SOMEVAR is not numeric"
fi

should do the trick, assuming SOMERVAR has been defined and is not an empty
string, though I haven't tested this.

Ta

Ta

 
 
 

Verify a command line argument as NUMERIC

Post by gary » Tue, 22 Feb 2000 04:00:00


an alternate solution might be to use the following pattern match in a
korn shell test.  this is testing positional parameter 1 equal to at
least one numeric digit, but regardless all characters in pos param 1
must be numeric.

  [[  "$1"  =  +([0-9])  ]]

cheers,
gary


>Can anyone help me with a general shell scripting question?  I need to be able
>to verify an argument passed via command line and ensure that the value is
>numeric.  Is there any way to do this?

>Thanks


 
 
 

Verify a command line argument as NUMERIC

Post by Chri » Tue, 22 Feb 2000 04:00:00



> Can anyone help me with a general shell scripting question?  I need to be able
> to verify an argument passed via command line and ensure that the value is
> numeric.  Is there any way to do this?

> Thanks


Any specific  kind of number?

--
Chris

 
 
 

Verify a command line argument as NUMERIC

Post by tya.. » Tue, 22 Feb 2000 04:00:00


Thanks for all the suggestions.  I was attempting to do something in AWK or SED
but could not seen to get anything to work.  I really need a regular expressions
book!  The reply using "tr -d" is interesting.  I never would have thought of
that one.

I just need it to verify a integer value, no + or - or anything else to worry
about.

If anyone else has some other need way please pass it along.

Thanks


>Can anyone help me with a general shell scripting question?  I need to be able
>to verify an argument passed via command line and ensure that the value is
>numeric.  Is there any way to do this?

>Thanks


 
 
 

Verify a command line argument as NUMERIC

Post by Charles Dem » Wed, 23 Feb 2000 04:00:00



>Can anyone help me with a general shell scripting question?  I need to be able
>to verify an argument passed via command line and ensure that the value is
>numeric.  Is there any way to do this?

>Thanks


Yes, check if it matches any of the various numeric formats, if
it matches one, then it's numeric.

Which numeric formats did you have in mind?  

This is not a trivial question.  :-)

Chuck Demas
Needham, Mass.

--
  Eat Healthy    |   _ _   | Nothing would be done at all,

  Die Anyway     |    v    | That no one could find fault with it.

 
 
 

Verify a command line argument as NUMERIC

Post by Dan Merc » Wed, 23 Feb 2000 04:00:00




> Thanks for all the suggestions.  I was attempting to do something in AWK or SED
> but could not seen to get anything to work.  I really need a regular expressions
> book!  The reply using "tr -d" is interesting.  I never would have thought of
> that one.

> I just need it to verify a integer value, no + or - or anything else to worry
> about.

> If anyone else has some other need way please pass it along.

> Thanks


>>Can anyone help me with a general shell scripting question?  I need to be able
>>to verify an argument passed via command line and ensure that the value is
>>numeric.  Is there any way to do this?

>>Thanks


A var is an integer if it contains no non-numeric elements:

case $var in
   *[!0-9]*) # non-numeric;;
   '') # empty string;;
   *) # numeric;;
esac

This is portable for all bournish shells.  Shells that support
extended globbing like ksh and bash 2 can use the following syntax:

    if [[ $var = +([0-9]) ]];then echo integer;...

You need to turn on extended globbing in bash with shopt:

   shopt -s extglob

--
Dan Mercer

Opinions expressed herein are my own and may not represent those of my employer.

 
 
 

Verify a command line argument as NUMERIC

Post by brian hile » Thu, 24 Feb 2000 04:00:00



> Can anyone help me with a general shell scripting question?  I need to be able
> to verify an argument passed via command line and ensure that the value is
> numeric.  Is there any way to do this?

Expr(1)? tr(1)? Ksh and even bsh have sufficient regular expression
matching capability to recognize the general case of a floating
point format:

# isnum returns True if its argument is a valid number.
# The first pattern requires a digit before the decimal
# point, and the second after the decimal point.
# bsh: does not acount for: 0e0 (no decimal point)

case $1 in
 (?([-+])+([0-9])?(.)*([0-9])?([Ee]?([-+])+([0-9])))
        return 0 ;;
 (?([-+])*([0-9])?(.)+([0-9])?([Ee]?([-+])+([0-9])))
        return 0 ;;
 *)     return 1 ;;
esac

-Brian

 
 
 

1. How can I pass a command line argument to an aliased command?

How can I pass a command line argument to an aliased command?

In this example, I want to pass a candidate directory name as a command line
argument and have it processed by the "dir" alias script.

For example:

arvant02:/opt/vantive8> alias dir='ll -F omu | egrep ^d '
arvant02:/opt/vantive8> dir
drwxrwxr-x       2 vantive    dba           2048 Aug  9  2001 apps_ora8/
drwxrwxrwx       2 vantive    dba           2048 Apr 26 11:46 backup/
drwxrwxrwx       2 oracle     dba             96 Oct  9  2001 core_22352/
drwxrwxrwx       2 vantive    dba           2048 Apr 26 11:38 data/
drwxrwxrwx       2 vantive    dba           4096 Apr 26 11:46 log/
drwxrwxr-x     8 vantive    dba             96 Aug  9  2001 movebinary/
drwxrwxr-x   3 vantive    dba             96 Apr 26 11:32 omudata/
drwxrwxr-x   2 vantive    dba           2048 Aug  9  2001 views/
arvant02:/opt/vantive8>

The above alias lists those directories belonging to the subdirectory omu.
I hardcoded the "omu" directory into the dir alias script.

If,however, I want to replace the hardcoded "omu" subdirectory value with a
variable that I pass in as a command line argument to the alias command
"dir", such as, dir sub_dir <enter>, how would I alter the alias script to
enable this?

Mark

2. another shit post from flattie

3. ?how to verify mksysb backup at command line

4. Does solaris have a built-in bootloader...?

5. Verify password from command line

6. installing w/o CD

7. HELP - RH Apache and command line arguments

8. : NeXT display on X11/DGS? NSHosting under Linux? HELP!

9. insmod command line argument

10. pagenumber as command line argument

11. Command line arguments, indirectly

12. How to mask command line arguments in C shell?

13. Quoting Last Command Line Argument