if $varib contains non-numeric charctr then...


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting if $varib contains non-numeric charctr then...
# 1  
Old 11-05-2008
if $varib contains non-numeric charctr then...

I've asked a similiar question to this, but this time it is different, a little more restrictive....I have a variable, $varib, that should always only contain numeric characters. I need a way to echo something if the variable contains a letter, space, or some other punctuation character, ,.!>?" etc.
Previously I used: grep '[a-zA-Z]' and that can work for letters, but what about the other characters?

#!/usr/bin/bash
#my checkbk
#input
echo "input number"
read varib
#if $varib contains a letter, space, comma, period, or other character
#echo something
#exit 1
#else
#echo good number, thanks.
# 2  
Old 11-05-2008
Quote:
Originally Posted by ajp7701
I've asked a similiar question to this, but this time it is different, a little more restrictive....I have a variable, $varib, that should always only contain numeric characters. I need a way to echo something if the variable contains a letter, space, or some other punctuation character, ,.!>?" etc.
Previously I used: grep '[a-zA-Z]' and that can work for letters, but what about the other characters?

Please put code inside [code] tags.
Quote:
Code:
#!/usr/bin/bash
#my checkbk
#input
echo "input number"
read varib
#if $varib contains a letter, space, comma, period, or other character
#echo something
#exit 1
#else
#echo "good number, thanks."


Code:
 case $varib in
     *[!0-9]*) echo something; exit 1 ;;
     *) echo good number, thanks. ;;
 esac

# 3  
Old 11-05-2008
What about just negotiation: grep [^0-9] :
Code:
if $(echo $varib|grep [^0-9] &>/dev/null) ; 
then echo WRONG_CHARS_FOUND; 
else echo NOTHING_WRONG; fi

But, generally, why not to check every charactr for any rules:
Code:
var_sz=${#varib}
for (( i=1; i<=var_sz; i++ )); do
  char=$(echo $varib|cut  -c$i); #or : char=${varib:$i-1:1}
  #-now check any validity
done

Although, just to check a valid number, I would prefer that way:
Code:
if ((10#${varib}==$varib)) &>/dev/null ; 
then echo A_NUMBER; 
else ec NOT_A_NMB; fi

Any questions - ask, I will explain.

Last edited by alex_5161; 11-05-2008 at 10:04 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help with numeric manupulations

I want to calculate the following expression in script: val=179584.0 ($val *3*16384)/(1073741824) $val will have a floating value. Thanks! I tried with following: n=$(echo |awk '{ print $val*3*16384 }') n=$(echo |awk '{ v=$val; print $v*3*16384 }') expr $val \* 3.0 \*... (10 Replies)
Discussion started by: karumudi7
10 Replies

2. Shell Programming and Scripting

How to check for a Numeric Value?

Using shell, I have a variable, how can I check that variable for a numeric value such as "41.0"? My program needs to do one things if the numeric value is found, and another if something else such as a string of letter is found. is there a specific character that denotes a numeral? The... (2 Replies)
Discussion started by: chagan02
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. UNIX for Dummies Questions & Answers

First argument is numeric or not

Hi everyone, I want my script to check if the first argument has only numbers or not. Im not sure what im doing wrong. This is how it looks like: if *") ] then echo 'The first arguement should only be in numeric' 1>&2 exit 1 else exit 0 fi (7 Replies)
Discussion started by: darkhider
7 Replies

5. Shell Programming and Scripting

Numeric or not?

Is there an easy way using a command or other routine for testing whether an argument on the command line is numeric? Just want to validate... I ran a search and didn't find a similar question.... Thx (3 Replies)
Discussion started by: harrisjl
3 Replies

6. Shell Programming and Scripting

Numeric or not

Is there a simple way of determining whether a command line arguement is numeric or not?? I tried a search and didn't find anything similar... This is the 2nd time I've made this post.... It didn't appear that the first one showed up ?? Sorry if this is a duplicate... (1 Reply)
Discussion started by: harrisjl
1 Replies

7. Shell Programming and Scripting

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. echo $1 | grep "\{2\}$" if then (12 Replies)
Discussion started by: sumesh.abraham
12 Replies

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

9. Shell Programming and Scripting

Checking numeric value

Hi, How can I check numeric value in shell? I am passing one parameter (integer) and I want to restrict any other characters except 0-9. ./script xyz 10 Here 2nd parameter (10) should be integer only. Can anyone help on this? Malay (6 Replies)
Discussion started by: malaymaru
6 Replies

10. UNIX for Dummies Questions & Answers

non-numeric argument

quick question, I am trying to run this simple equation expr 2048 / 2.354 but get a "expr: non-numeric argument" error when ever its run. I believe it may be caused by the decimal point but I do not know how to remedy it. (3 Replies)
Discussion started by: TiredOrangeCat
3 Replies
Login or Register to Ask a Question