The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
check the input Shilpi UNIX for Dummies Questions & Answers 1 03-04-2008 05:26 AM
check input = "empty" and "numeric" geoffry Shell Programming and Scripting 6 12-13-2007 02:12 AM
How to check for a valid numeric input Vijayakumarpc Shell Programming and Scripting 1 08-04-2007 05:34 AM
How to prompt for input & accept input in ONE line newbie168 Shell Programming and Scripting 2 09-27-2005 02:02 AM
What can i do to check that the input is all alphabet.. ? XXXXXXXXXX Shell Programming and Scripting 4 02-19-2002 04:09 PM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 03-10-2006
Registered User
 

Join Date: Mar 2006
Posts: 35
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
Reply With Quote
Forum Sponsor
  #2  
Old 03-10-2006
blowtorch's Avatar
Supporter
 
Join Date: Dec 2004
Location: Singapore
Posts: 2,326
What shell are you using? If you are using ksh, you can use typeset -i. Here's how:
Code:
$ 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.
Reply With Quote
  #3  
Old 03-13-2006
Registered User
 

Join Date: Mar 2006
Posts: 35
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..

Quote:
Originally Posted by blowtorch
What shell are you using? If you are using ksh, you can use typeset -i. Here's how:
Code:
$ 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.
Reply With Quote
  #4  
Old 03-13-2006
blowtorch's Avatar
Supporter
 
Join Date: Dec 2004
Location: Singapore
Posts: 2,326
A different approach.
Code:
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.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 10:26 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0