Numeric Validation


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Numeric Validation
# 1  
Old 12-19-2006
Numeric Validation

Hi,

How to check whether an input to a shell script contain only numeric values.
Is there any way to check against the following characters.
&
(
)
|
\
"
'
<
>
`
I've used the following way.

Code:
echo $1 | grep "[^0-9]\{2\}$"
if [ $? -eq 0 ]
then
echo "OK"
else
echo "Error"
fi

But, in this case there are two issues.

1) If the input contains numeric values the input value will be displayed which i don't want.
2) If special characters mentioned above are included, the script fails.

I've given up. I would really appreciate if anyone can help me out.

Thanks,
Sumesh
sumesh.abraham
# 2  
Old 12-19-2006
grep "[^0-9]\{2\}$" > /dev/null
if [ $? -eq 0 ]
then
echo "OK"
else
echo "Error"
fi
# 3  
Old 12-19-2006
1) If the input contains numeric values the input value will be displayed which i don't want.

Code:
echo $1 | grep "[0-9]\{2\}$" > /dev/null 

or
Code:
if  echo $1 | grep -q "[0-9]\{2\}$"
then
echo "OK"
else
echo "Error"
fi

2) If special characters mentioned above are included, the script fails.

Use quotes for special characters
Code:
ur_script '&'
ur_script \'


Last edited by anbu23; 12-19-2006 at 09:09 AM..
# 4  
Old 12-19-2006
Aju,Anbu

Thanks for the reply.

I tried your code, but still not through.

I've to do grep against $1 and make sure that it should contain two digits of 0-9 range.

I tried to execute the script like

Code:
./shellpgm.sh `

In cases like these, error is thrown.

Pls help me.

Thanks,
Sumesh
sumesh.abraham
# 5  
Old 12-19-2006
Quote:
Originally Posted by sumesh.abraham
Aju,Anbu

Thanks for the reply.

I tried your code, but still not through.

I've to do grep against $1 and make sure that it should contain two digits of 0-9 range.

I tried to execute the script like

Code:
./shellpgm.sh `

In cases like these, error is thrown.

Pls help me.

Thanks,
Sumesh
I have answered for this in previous reply. Use \`
# 6  
Old 12-19-2006
echo $1 | grep "^[0-9]\{2\}$" > /dev/null
if [ $? -eq 0 ]
then
echo "OK"
else
echo "Error"
fi

use carret (^) outside brackets
# 7  
Old 12-19-2006
Thanks Anbu. When i try executing the script with input preceeded by \ it works. Can u pls tell me how to concatenate \ with $1 and proceed.

Thanks a lot
Sumesh
sumesh.abraham
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Validation using While and IF

I am learning Shell scripting on own. I am trying to do an assignment to get details from the user like username their individual marks ,DOB and send a report in mail with the Details calculated like total and average. validate_marks() { local Value=$1 if && then return 0 else... (1 Reply)
Discussion started by: JayashreeRobin
1 Replies

2. Shell Programming and Scripting

Value validation

Hi All I am trying to validate a value using if condition requirement is need to check whether its a valid numeric value the input contains ( space, #N/A and negative and positive decimal values and Zeros) if it contains the space, I need to display the error message as space ... (15 Replies)
Discussion started by: tsurendra
15 Replies

3. Windows & DOS: Issues & Discussions

Put the numeric validation in user input when value is 5.1.2.3

Hi I need to put the validation in batch script when user will enter the build number it should be numeric.I can put the validation for numeric values but there is .(dot) in number so it would not take it as numeric. Is it possible we can store it in variable and remove the .(dot) from the... (1 Reply)
Discussion started by: anuragpgtgerman
1 Replies

4. Shell Programming and Scripting

Name validation

Hi All, I need to write a small piece of code to check the following. name should contain (A-Z), spaces, hyphens & apostrophes I need to generate regular expressions for the same. Please help me out as i am not familiar with regular expressions. (1 Reply)
Discussion started by: lifzgud
1 Replies

5. Shell Programming and Scripting

Validation help

Hi, I am new to Unix shell scripting and need help to add some validation to an existing script. I've made a script that takes two argument (input) but I want the script to display an error message when nothing (null) is entered. So far I managed to validate the fist argument but fail to... (2 Replies)
Discussion started by: zen10
2 Replies

6. UNIX for Dummies Questions & Answers

Find and Replace random numeric value with non-numeric value

Can someone tell me how to change the first column in a very large 17k line file from a random 10 digit numeric value to a non numeric value. The format of lines in the file is: 1702938475,SNU022,201004 the first 10 numbers always begin with 170 (6 Replies)
Discussion started by: Bahf1s
6 Replies

7. Web Development

Validation in php help !

I am validating a form using php script and I want to "echo" the error message near to the text box itself & not below all the controls....Can I Position the display messages ?..Pls help me... (2 Replies)
Discussion started by: gameboy87
2 Replies

8. UNIX for Dummies Questions & Answers

Validation

I'm kinda new in shell scripting. How do i validate an input from a user to conform to requirement. For example, echo "Enter First Name: " read FName echo "Enter Date of Employment (dd/mm/yyyy): " read DoE If the user enters data that is alphanumeric, it accepts it. I hope i've... (1 Reply)
Discussion started by: Allenzo
1 Replies

9. Shell Programming and Scripting

Perl code to differentiate numeric and non-numeric input

Hi All, Is there any code in Perl which can differentiate between numeric and non-numeric input? (11 Replies)
Discussion started by: Raynon
11 Replies
Login or Register to Ask a Question