Check on Input

HI all,

I would like to know how the user can be restricted for entering only the number and not characters in sheel scripts..

Suppose code is like this

echo 'Enter the number'
read Value

Now user may enter 'a' as value...
But i want to disallow him for entering characters other than numbers.

How can i do it...
Is their anything like
value=\^D

Please specify
Thanks

What shell are you using? If you are using ksh, you can use typeset -i. Here's how:

$ typeset -i a
$ echo $a

$ a=a
$ echo $a
0
$ a=10
$ echo $a
10

A non-integer will be automatically set to 0.

  • note: this is tested on FreeBSD.

Hi,

The code u send is working fine...
Now it is not giving an error expr : as non-numeric argument
I am working on Ksh

But if i wanna do following

$ User prompted -> Enter the number
$ He enters -> b

now instead of accepting 'b' ( as it is not an integer )
I want to clear that 'b' ...as follows and again prompt the user for entering the value

$ User prompted -> Enter the number
$ He enters -> b

( clear that 'b' or any character unless and until he enters integer )

$ User prompted -> Enter the number

Thanks..

A different approach.

echo "enter string: \c"
read a
A=$(echo $a |tr -d '[0-9]*') 
if [ -z $A ]; then
   echo number!
else
   echo not number!
fi

Search the site for 'string validation' to get some more examples.