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




Chris F.A. Johnson のメッセージ:

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.
why?
if i can chose, i'd use python. yes, ...maybe i can chose.


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

it works, thanks.


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

.



Relevant Pages

  • Re: Random selection
    ... > I'm working on a script that requires the computer to chose a random ... > executes, multiplying 3 times the randomly generated number and then adds ...
    (microsoft.public.scripting.vbscript)
  • Re: Problem with Exit Status
    ... I am trying a script with the following logic in bash: ... in the first line you included spaces flanking the '=' character. ... Awk will happily accept ''. ... Shell Scripting Recipes: | My code in this post, if any, ...
    (comp.unix.shell)
  • Random selection
    ... I'm working on a script that requires the computer to chose a random number, ... I looked in a vbscript source book and it gives the ... Thanks, Jim ...
    (microsoft.public.scripting.vbscript)
  • Math problem
    ... I'm working on a script that requires the computer to chose a random number, ... I looked in a vbscript source book and it gives the ... Thanks, Jim ...
    (microsoft.public.scripting.vbscript)
  • Re: Foreigner(s)
    ... Rex Harrison knew of the solecisms in the script, of course, but ... chose to turn a blind eye to them....r ... And Sammy Davis, Jr., in a conversation about golf, was once asked, ...
    (alt.usage.english)