LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   how can i check that $1 is number... (https://www.linuxquestions.org/questions/programming-9/how-can-i-check-that-%241-is-number-710028/)

DoME69 03-08-2009 09:29 AM

how can i check that $1 is number...
 
HI ...

how can i check at cshell that $1 is number and not contain letters

Thanks

pixellany 03-08-2009 09:36 AM

If the c-shell is like BASH, then numbers are stored as text strings.

echo $1 | grep -v [0-9]

DoME69 03-08-2009 09:48 AM

Thanks...

but this grep can' find

$1 = 98y88

pixellany 03-08-2009 10:00 AM

OOPS!!

grep -v [0-9] finds all lines that do not contain any numbers.

grep -v [^0-9] finds all lines that do not contain any characters that are not numbers

Hko 03-08-2009 10:07 AM

Quote:

Originally Posted by DoME69 (Post 3468667)
how can i check at cshell that $1 is number and not contain letters

...
Quote:

Originally Posted by DoME69 (Post 3468667)
but this grep can' find

$1 = 98y88

True, but "98y88" isn't a number, as it does contain letters...

DoME69 03-08-2009 01:24 PM

how can i set, if there is letters at $1
set b = 1

else

set b = 0

?

Thanks again.

Hko 03-08-2009 02:15 PM

I don't know anything about cshell (sorry).
But here are 3 different ways to do it in bash. Hope this helps you to translate it to cshell.
Code:

if echo $0 | grep -q '^[0-9]*$'; then
    b=1
else
    b=0
fi

Code:

echo $0 | grep '^[0-9]*$' && b=1 || b=0
Code:

echo $0 | grep -qv '^[0-9]*$'
b=$?


DoME69 03-08-2009 02:37 PM

Thanks for your effort
but i need it at cshell
:)

Hko 03-08-2009 02:42 PM

Quote:

Originally Posted by DoME69 (Post 3468875)
Thanks for your effort
but i need it at cshell

Shouldn't be too difficult to translate from these bash examples. The grep command can stay the same at least.

AnanthaP 03-09-2009 05:55 AM

if expr $1/1 == $1 then number else not a number

Should work at any shell. BTW, at least lookup the exact format for expr.

ATB

End

sureshsujatha 03-09-2009 02:08 PM

@AnantaP,
Wont the expression fail if the field inside $1 is not a number?


All times are GMT -5. The time now is 12:01 AM.