validating variables (numeric)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting validating variables (numeric)
# 1  
Old 11-28-2006
validating variables (numeric)

Hi

I need to get a user to enter a number (an exchange rate) into a script. I have the following:

#!/bin/ksh
echo "Enter exchange rate:"
read EX_RATE
if [ -z "`echo \"$EX_RATE\" | tr -d \"[:digit:]\"`" ]
then
echo "Well done - only numeric here"
else
echo "Not so well done - there is NON numeric stuff here!"
fi

This works fine except when I try to enter a decimal number when it exits because the decimal point is obviously non-numeric. I have looked at the other class keywords and there is not one that covers decimals.

Can anyone help me out here?

thanks

Paul
# 2  
Old 11-28-2006
Quote:
Originally Posted by pjd1
if [ -z "`echo \"$EX_RATE\" | tr -d \"[:digit:]\"`" ]
Code:
if [ -z "`echo \"$EX_RATE\" | tr -d \"[0-9\.]\"`" ]

# 3  
Old 11-28-2006
try this
Code:
echo "Enter exchange rate:"
read EX_RATE
if [ -n "`echo \"$EX_RATE\" | sed -n "/^[0-9.]\{1,\}$/p"`" ]
then
echo "Well done - only numeric here"
else
echo "Not so well done - there is NON numeric stuff here!"
fi

# 4  
Old 11-28-2006
many thanks guys - both answers work a treat.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Increase two numeric variables incrementally together

Hi, I was wondering if someone could help with what is probably a fairly easy problem. I have two variables, i is between 1-5, j is between 11-15 I'd like to produce this: 1_11 2_12 3_13 4_14 5_15 Each number goes up incrementally with the other. But my shoddy code is not... (5 Replies)
Discussion started by: hubleo
5 Replies

2. Shell Programming and Scripting

awk for validating

HI, I am trying to write a validation script as below awk '($1=="ABC"&&$2="XYZ" ,then check the value in $8,$10 these columns should not be null. so their should be some corresponding value n the $8 and $10 column,if their is no value the script has to give error saying that at a... (2 Replies)
Discussion started by: gaur.deepti
2 Replies

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

4. Shell Programming and Scripting

validating a input file for numeric and character

i have a input file like this 001|rahim|bajaj|20090102 while reading the file i need to check whether the first column is a number second column is a name is there any methodology to check for the same thanks in advance (2 Replies)
Discussion started by: trichyselva
2 Replies

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

6. Shell Programming and Scripting

validating data

I have a file like the following aaaaa00005bbbbb aaaaa00108bbbbb The code "00005" and "00108" need to be validated and the list of valid codes are stored in a database. While I loop through the file, should call a sql statement for every records to do the validation? or is... (1 Reply)
Discussion started by: joanneho
1 Replies

7. Shell Programming and Scripting

Validating variables in shells script

All Can you help me to validate a variable only for string and digit. That is variable should either fully alphabets or digits. Please send me result to my mail id also: REMOVED Thanx in advance Regards Deepak Xavier (1 Reply)
Discussion started by: DeepakXavier
1 Replies

8. Shell Programming and Scripting

Validating $1 and $2 before using

Hi there, I'm trying to validate my $1 and $2 before I use them in the sqlplus update, I think I have the validation rules set correctly??? however when I test my script, it jumps straight into the sql. It doesn't seem to validate the vars....even when I know they are incorrect. if ; then ... (6 Replies)
Discussion started by: nhatch
6 Replies

9. UNIX for Dummies Questions & Answers

validating userid

What's wrong with this syntax? It's part of my 'if' statement but it doesn't seem to pass and it keeps going to the 'else' part. I thought it says that userid must start with a non-numeric character and is between 6 and 10 characters long (alphanumeric). $userid|grep -Eq '^?\{6,10\}+$' if... (2 Replies)
Discussion started by: giannicello
2 Replies

10. Programming

validating input

how do i validate y script so that it only accepts values between 1 and 3 and against any character input, cause at the moment i can only validate against numbers outside 1 and 3 but not characters cheers (4 Replies)
Discussion started by: ruffenator
4 Replies
Login or Register to Ask a Question