Unix Shell - newbie question: how to convert str to int in c-shell?

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > October 2006 > newbie question: how to convert str to int in c-shell?





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 newbie question: how to convert str to int in c-shell?
neo

2006-10-31, 1:31 am

hi, i am a newbie in shell. i am using c shell.
say convert a argument to int.
if it is not a int , some flag or message should be got.
how to do so in a neat way?
thanks.

Chris F.A. Johnson

2006-10-31, 7:22 am

On 2006-10-31, neo wrote:
> hi, i am a newbie in shell. i am using c shell.


If you want to write a script, don't use csh.

> say convert a argument to int.
> if it is not a int , some flag or message should be got.
> how to do so in a neat way?


A simple test for a numeric argument, in a standard shell is:

case $n in
*[!0-9]*) printf "%s\n" "Not a number" ;;
esac


--
Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
Stephane CHAZELAS

2006-10-31, 7:22 am

2006-10-31, 03:07(-05), Chris F.A. Johnson:
> On 2006-10-31, neo wrote:
>
> If you want to write a script, don't use csh.
>
>
> A simple test for a numeric argument, in a standard shell is:
>
> case $n in
> *[!0-9]*) printf "%s\n" "Not a number" ;;


"" | *[!0-9]*) ...

for unsigned int, assuming you don't want to check for
boundaries (too big numbers) and that 089 would not be taken as
an invalid octal number.

> esac




--
Stéphane
neo

2006-10-31, 7:22 am


Chris F.A. Johnson =E3=81=AE=E3=83=A1=E3=83=83=E3=82=BB=E3=
83=BC=E3=82=B8:

> On 2006-10-31, neo wrote:
>
> If you want to write a script, don't use csh.

why?
if i can chose, i'd use python. yes, ...maybe i can chose.

>
>
> A simple test for a numeric argument, in a standard shell is:
>
> case $n in
> *[!0-9]*) printf "%s\n" "Not a number" ;;
> esac
>

it works, thanks.

>
> --
> Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell>
> Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
> =3D=3D=3D=3D=3D My code in this post, if any, assumes the POSIX locale
> =3D=3D=3D=3D=3D and is released under the GNU General Public Licence


Ed Morton

2006-10-31, 7:22 am

neo wrote:
> Chris F.A. Johnson ã®ãƒ¡ãƒƒã‚»ãƒ¼ã‚¸:
>
>
>
> why?


See question 17 in the FAQ
(http://home.comcast.net/~j.p.h/cus-faq-2.html#17).

Ed.
Chris F.A. Johnson

2006-10-31, 7:25 pm

On 2006-10-31, neo wrote:
>
> Chris F.A. Johnson ??????:
>
> why?


See these articles:

<http://www.grymoire.com/Unix/CshTop10.txt>
<http://www.grymoire.com/Unix/Csh.html#uh-0>
<http://www.faqs.org/faqs/unix-faq/shell/csh-whynot/>


--
Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com