Unix Shell - test if string is a number

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > January 2006 > test if string is a number





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author test if string is a number
jimmij

2006-01-13, 10:40 pm

Hi,
I found in google this solution:

case $var in
*[!0-9]*) echo string;;
*) echo NUMBER ;;
esac

It works ok, however I wish to use "if" istead of "case".
It sounds simple but I cannot manage it by myself.

Thanks for any help,
jimmij
--
I come from the Land Of Imagination
Janis Papanagnou

2006-01-13, 10:40 pm

jimmij wrote:
> Hi,
> I found in google this solution:
>
> case $var in
> *[!0-9]*) echo string;;
> *) echo NUMBER ;;
> esac
>
> It works ok, however I wish to use "if" istead of "case".
> It sounds simple but I cannot manage it by myself.
>
> Thanks for any help,
> jimmij


Your shell, whichever it may be, might support this...

if [[ $var = +([0-9]) ]] ; then print number ; fi

....depending on how number is defined for you.
It won't recognize, e.g., 1.0e12

Janis
Benjamin Schieder

2006-01-13, 10:40 pm

jimmij wrote:
> Hi,
> I found in google this solution:
>
> case $var in
> *[!0-9]*) echo string;;
> *) echo NUMBER ;;
> esac
>
> It works ok, however I wish to use "if" istead of "case".
> It sounds simple but I cannot manage it by myself.


if [ -n "${var//[^0-9]/}" ] ; then
echo string
else
echo NUMBER
done

Greetings,
Benjamin

--
_ _ _ _ _
| \| |___| |_| |_ __ _ __| |__
| .` / -_) _| ' \/ _` / _| / /
|_|\_\___|\__|_||_\__,_\__|_\_\
| | (_)_ _ _ ___ __
| |__| | ' \ || \ \ /
|____|_|_||_\_,_/_\_\
Play Nethack anywhere with an x86 computer:
http://www.crash-override.net/?nethacklinux
Chris F.A. Johnson

2006-01-13, 10:40 pm

On 2006-01-11, jimmij wrote:
> Hi,
> I found in google this solution:
>
> case $var in
> *[!0-9]*) echo string;;
> *) echo NUMBER ;;
> esac
>
> It works ok, however I wish to use "if" istead of "case".
> It sounds simple but I cannot manage it by myself.


Why? The correct way is to use case.

Bash and ksh93 have [[...]] for pattern matching, but it's not
portable.

Otherwise, you need an external command such as expr.

--
Chris F.A. Johnson, author | <http://cfaj.freeshell.org>
Shell Scripting Recipes: | My code in this post, if any,
A Problem-Solution Approach | is released under the
2005, Apress | GNU General Public Licence
Stachu 'Dozzie' K.

2006-01-13, 10:40 pm

On 11.01.2006, jimmij <jimmij@jj.jj> wrote:
> Hi,
> I found in google this solution:
>
> case $var in
> *[!0-9]*) echo string;;
> *) echo NUMBER ;;
> esac
>
> It works ok, however I wish to use "if" istead of "case".
> It sounds simple but I cannot manage it by myself.
>
> Thanks for any help,


I see you copy your aversion to using documentation
(<rhwbqz8suk1.fsf@jj.jj> on pl.comp.os.linux) to using the Google for
longer than 30 seconds. Recently there was thread with exactly the same
question as yours. There were few posts "why using `if' when `case' is
better for that?", with which I agree. Even more, there was a solution
using `if' and explaination why this is worse than `case'!

--
Feel free to correct my English
Stanislaw Klekot
Kevin Collins

2006-01-13, 10:40 pm

In article <43c54e37$0$20777$9b4e6d93@newsread4.arcor-online.net>, Benjamin
Schieder wrote:
> jimmij wrote:
>
> if [ -n "${var//[^0-9]/}" ] ; then
> echo string
> else
> echo NUMBER
> done


That is very shell-dependant code. It won't work in ksh88. Actually, I tested
using bash and discovered it won't work at all.

First of all, the "done" should be "fi". Secondly, this code ALWAYS prints
"string" unless $var is undefined or empty.

Kevin

--
Unix Guy Consulting, LLC
Unix and Linux Automation, Shell, PERL and CGI scripting
http://www.unix-guy.com
Benjamin Schieder

2006-01-13, 10:40 pm

Kevin Collins wrote:
> In article <43c54e37$0$20777$9b4e6d93@newsread4.arcor-online.net>, Benjamin
> Schieder wrote:
>
>
>
> That is very shell-dependant code. It won't work in ksh88. Actually, I tested
> using bash and discovered it won't work at all.
>
> First of all, the "done" should be "fi". Secondly, this code ALWAYS prints
> "string" unless $var is undefined or empty.


Strange. I remember using this form to check for a string. Let me search for it.

Greetings,
Benjamin

--
Benjamin 'blindCoder' Schieder
Registered Linux User #289529: http://counter.li.org
finger blindcoder@scavenger.homeip.net | gpg --import
--
/lusr/bin/brain: received signal: SIGIDIOT
Benjamin Schieder

2006-01-13, 10:40 pm

Benjamin Schieder wrote:
> Kevin Collins wrote:
>
>
>
> Strange. I remember using this form to check for a string. Let me search for it.


Okay, wrong mistake. I _did_ use that form, but it's wrong here.

What we need is this:

if [ -n "${var//[0-9]/}" ] ; then
echo string
else
echo NUMBER
fi

Greetings,
Benjamin

--
Today, memory either forgets things when you don't want it to,
or remembers things long after they're better forgotten.
Stephane CHAZELAS

2006-01-13, 10:40 pm

2006-01-11, 19:08(+01), jimmij:
> Hi,
> I found in google this solution:
>
> case $var in
> *[!0-9]*) echo string;;
> *) echo NUMBER ;;
> esac
>
> It works ok, however I wish to use "if" istead of "case".
> It sounds simple but I cannot manage it by myself.

[...]

You forgot to treat the empty string as a "string".

is_a_number() {
case $1 in
*[!0-9]* | "") false;;
*) true;;
esac
}

if is_a_number "$var"
then
echo NUMBER
else
echo string
fi

if expr "x$var" : 'x[0-9][0-9]*$'; then
then
echo NUMBER
else
echo string
fi

--
Stéphane
jimmij

2006-01-13, 10:40 pm

Benjamin Schieder <blindcoder@scavenger.homeip.net> writes:

> Benjamin Schieder wrote:
>
> Okay, wrong mistake. I _did_ use that form, but it's wrong here.
>
> What we need is this:
>
> if [ -n "${var//[0-9]/}" ] ; then
> echo string
> else
> echo NUMBER
> fi


The last piece of code works very well in my GNU bash,
version 2.05b.0(1)-release (i686-pc-linux-gnu)

thx,
jj
--
I come from the Land Of Imagination
Ed Morton

2006-01-13, 10:40 pm



jimmij wrote:

> Benjamin Schieder <blindcoder@scavenger.homeip.net> writes:

<snip>
>
>
> The last piece of code works very well in my GNU bash,
> version 2.05b.0(1)-release (i686-pc-linux-gnu)


Try it with var="", var="3.14", and var="-1".

Ed.
jimmij

2006-01-13, 10:40 pm

Ed Morton <morton@lsupcaemnt.com> writes:

> jimmij wrote:
>
> <snip>
>
> Try it with var="", var="3.14", and var="-1".


I did. In priciple it doesn't properly manage these cases, but in my
script it is ok, because I _know_ that $var has at least one character.
Also from my point of view "3.14" and "-1" is a string (my numbers
should be only unsigned integer).

I know of course that it can be written better, but it is good enough
for me.

Thanks all of you for help,
jimmij
--
I come from the Land Of Imagination
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com