Hi Charles,
On Tue, 2003-07-08 at 13:32, Charles Steinkuehler wrote:
> Peter Nosko wrote:
> > pn] Forgive me; I can't Google this for some reason.
> >
> > pn] Is there an elegant way to test a positional parameter for being numeric (so
> > that I don't
> > assign a string to a numeric variable)?
>
> The quick & dirty way to do something like this would be with a case
> statement:
>
> case $1
> in
> [0-9]*) echo Number: $1 ;;
> *) echo Text: $1 ;;
> esac
Quite simple and elegant, however...
> Note that this only tests the *FIRST* character of the parameter, so
> something like 1a would incorrectly look like a number.
>
> To get around this problem (if necessary), you'll either need to
> recursively parse each digit of the parameter to see if it's a number
> (ugly, but relies only on built-in shell commands)...something like:
>
> <example>
>
> #!/bin/ash
>
> ParseChar () {
> case $1
> in
> [0-9]*)
> if [ ${#1} -ge 2 ] ; then
> Parse ${1#?}
> fi ;;
> *) NUMBER=NO ;;
> esac
> }
>
> Parse () {
> NUMBER=YES
> ParseChar $1
> }
>
> Parse $1
>
> echo "Number?: $NUMBER"
>
> </example>
Charles, you are a scary man! Nobody should actually be able to do this
without having to refer to a number of documents. Do you dream in bash
scripts? :)
- HiltonT
-------------------------------------------------------
This SF.Net email sponsored by: Parasoft
Error proof Web apps, automate testing & more.
Download & eval WebKing and get a free book.
www.parasoft.com/bulletproofapps
------------------------------------------------------------------------
leaf-user mailing list: [EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/leaf-user
SR FAQ: http://leaf-project.org/pub/doc/docmanager/docid_1891.html