|
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?
|
|
|
| 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
| |
|
|
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
| |
|
|
|
|
|
|
|