Visit Our UNIX and Linux User Community

Is there any way to makesure that the input from the user is all numbers?

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Is there any way to makesure that the input from the user is all numbers?
# 1  
Old 01-27-2002
Is there any way to makesure that the input from the user is all numbers?

Is there any way to makesure that the input from the user is all numbers?

# 2  
Old 01-28-2002
if expr "$answer" : "[0-9]*$" > /dev/null ; then
echo 'all digits'
echo 'NOT all digits'
# 3  
Old 01-28-2002
Originally posted by Jimbo
if expr "$answer" : "[0-9]*$" > /dev/null ; then
echo 'all digits'
echo 'NOT all digits'
# 4  
Old 02-10-2002
Originally posted by Jimbo
if expr "$answer" : "[0-9]*$" > /dev/null ; then
echo 'all digits'
echo 'NOT all digits'
what does the *$ after the [0-9] means ?
and what does > /dev/null means
i know that if i never put the > /dev/null , if all the user input is digits, it will display out now many digits are there.
can explain to me what does these means ?

thank you
# 5  
Old 02-11-2002
This form of expr returns the number of characters in a string that match the specified pattern. In the following commands, the dot represents any single character, and the following asterisk means zero or more of that character:

expr XY : "X.*Y" (returns value of 2)
expr XabcY : "X.*Y" (returns value of 5)
expr XabcYdef : "X.*Y" (returns value of 5)

So the command:

if expr "$answer" : "[0-9]*$"

is looking for a digit, zero-or-more times, with nothing else following, because the $ means end-of-line or in this case end-of-string. And for expr, there is an implied beginning-of-line anchor.

Since you are not interested in seeing the number of characters that qualify, but only in the true/false result of the if-test, we discard the output of this command with the > /dev/null.

If the expression evaluates to one or more characters, it will return true, else will return false. So even though the asterisk allows zero-or-more occurrences in that position, a null answer evaluates to zero, thus false.

And if part of the pattern is enclosed in parentheses, if the entire expression evaluates true, instead of returning the number of characters that match the pattern, it will return the parenthesized part of that pattern. Example:

expr XabcYdef : "X\(.*\)Y"

returns "abc". That is a little hard to read because both the left and right parentheses must be preceeded with backslashes. Without the backslashes, it is easier to read:

expr XabcYdef : "X(.*)Y"

Last edited by Jimbo; 02-11-2002 at 12:11 PM..
# 6  
Old 02-19-2002
another way :)

you can also do this...

let test_number=$input_number+1 2>number_error.txt

if test -s number_error.txt
echo "$input_number is NOT a number"
echo "$input_number is a number"


Previous Thread | Next Thread
Test Your Knowledge in Computers #152
Difficulty: Easy
In 1995, Novell transferred the Unix trademarks to the ACME Computing Company.
True or False?

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

User input and run awk using the input

I am trying to allow a user to enter in text and then store that text in a variable $gene to run in an awk command in which those values are used to run some calculations. I am getting syntax errors however, when I try. Thank you :). The awk runs great if it is a pre-defined file that is used,... (7 Replies)
Discussion started by: cmccabe
7 Replies

2. Shell Programming and Scripting

Reducing the decimal points of numbers (3d coordinates) in a file; how to input data to e.g. Python

I have a file full of coordinates of the form: 37.68899917602539 58.07500076293945 57.79100036621094 The numbers don't always have the same number of decimal points. I need to reduce the decimal points of all the numbers (there are 128 rows of 3 numbers) to 2. I have tried to do this... (2 Replies)
Discussion started by: crunchgargoyle
2 Replies

3. Shell Programming and Scripting

Script interacts with user , based on user input it operates

i have a script which takes input from user, if user gives either Y/y then it should continue, else it should quit by displaying user cancelled. #!/bin/sh echo " Enter your choice to continue y/Y OR n/N to quit " read A if then echo " user requested to continue " ##some commands... (7 Replies)
Discussion started by: only4satish
7 Replies

4. Shell Programming and Scripting

How to get the user input recursively until the user provides valid input

Hi, echo "Enter file name of input file list along with absolute path : " read inputFileList if then for string in `cat inputFileList` do echo $string done else echo " file does not exist" fi From the above code, if the user enters a invalid file... (1 Reply)
Discussion started by: i.srini89
1 Replies

5. UNIX for Dummies Questions & Answers

Extraction of numbers from input

I have the following input: xxxx-2.7.19_WR3 I have extract the following: 2.7.19 Can anyone suggest an awk or sed command to do the above...:confused: another condition is if the input is : xxx-xxx_xxx-1.4-1_WR3.0bg.armv6jel_vfp it shud still extract : 1.4-1 whenever "-"... (12 Replies)
Discussion started by: xerox
12 Replies

6. Shell Programming and Scripting

input of two numbers in the middle of the script

Hi, My script ask for the input of two numbers in the middle of it. I would like to know how to indicate this two numbers when I submit the script. thanks for any help, jalves (4 Replies)
Discussion started by: jalves
4 Replies

7. Shell Programming and Scripting

Constructing numbers from input lines

I have a file with the information shown and I want to capture the entry having the rgdt tag and taking the value (the location of the represents the decimal point, for example 0p50 represents 0.50) I then want to divide the number at the end of each line by the value 0.50 I want to do... (7 Replies)
Discussion started by: kristinu
7 Replies

8. UNIX for Dummies Questions & Answers

Negative Numbers for input parameters.

Hello, I have a command that I need to supply a negative number as a parameter; how do I do this? I have tried giving it with double quotes, "", but no avail. Thanks, Gussi (3 Replies)
Discussion started by: Gussifinknottle
3 Replies

9. Shell Programming and Scripting

Accept user input - only numbers

I have a situation where I want the user to enter only numbers in response to a READ command. I have some validation to restrict the number to be between 1 and 12, but if the user type in some characters the script echoes some error message and goes to the next command. Below is a snippet of the... (1 Reply)
Discussion started by: pvar
1 Replies

Featured Tech Videos