Ksh How to test if variable is numeric??


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Ksh How to test if variable is numeric??
# 1  
Old 04-09-2008
Question Ksh How to test if variable is numeric??

I'm trying to test and see whether a variable that is being extracted from a line in a file is numeric or not. I have tried everything that I can think of and I cannot figure out how to get it to work. I am trying to test and see if the string extracted contains 5 numeric digits. This is what I have so far:

case $zipcode in
+([0-9])|+([0-9])|+([0-9])|+([0-9])|+([0-9]))
if [[ $zipcode -lt 1 ]] && [[ $zipcode -gt 99999 ]] || [[ $zipcount -ne 6 ]]
then
echo ${myline}
echo "ZIPCODE WARNING! -> ${zipcode}"
fi
;;
*)
echo ${myline}
echo "ZIPCODE WARNING! -> ${zipcode}"
;;
esac
# 2  
Old 04-09-2008
Code:
case $zipcode in [0-9][0-9][0-9][0-9][0-9]) ;;
    *) echo "$myline"; echo "zipcode warning: $zipcode" >&2;;
esac

If it's five digits then it can't be bigger than 99999 or less than 00000, so that check seems superfluous. Also you seem to have a logic error there (it cannot be less than one and bigger than 99999). The check for bigger than 0 still needs to be done but it complicates things a bit so I didn't want to dilute this example with the handling of that.

Last edited by era; 04-09-2008 at 04:11 PM.. Reason: make it more similar to original
# 3  
Old 04-09-2008
Sorry it was a little tedious, but I just tried it your way and it still doesnt work...it prints out zipcode warnings for every line in the file. even the ones that are legit...I'm not sure what we are doing wrong, but is there another way to do it?
# 4  
Old 04-09-2008
Do you have spaces around the digits? Is there a trailing newline?
# 5  
Old 04-09-2008
No there aren't any leading or trailing whitespaces or newlines in this variable. I'm not sure why it is giving me warnings for legit zipcodes with 5 digits, but is there another way to check and see if a variable is numeric?
# 6  
Old 04-09-2008
There are ways of course, but it would be better IMHO to understand what's wrong here. Does it pass strict numerical test? Does it pass five-character test?

Code:
case $zipcode in *[!0-9]*) echo did not pass all-number test >&2;; esac
case $zipcode in ?????) echo did not pass five-character test >&2;; esac

# 7  
Old 04-09-2008
I was able to get it to work...sorry it had one leading whitespace that was keeping it from recognizing that the variable was numeric and not a string. Thanks for all your help I really appreciate it.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Checking numeric expression in .ksh script

Gurus, Please need your help. I'm saving a filetimestamp into $filetimestamp and say....echo $filetimestamp gives 2015021612 I'm saving a cutoff_time into $cutoff_time say....echo $cutoff_time gives 2015021514 now my requirement is to check if $filetimestamp is greater than... (4 Replies)
Discussion started by: thummi9090
4 Replies

2. Shell Programming and Scripting

How to get a numeric value from Oracle to UNIX variable without spaces?

Hi, I am using the below code to get a numeric value from oracle to unix variable: BD_RC_CNT=`sqlplus -s ${WMD_DM_CONNECT} <<EOF set heading off set pagesize 0 Select count(*) from wmd_bad_data where proc_id = ${PROC_ID} and file_id = ${FILE_ID} and file_dt =... (7 Replies)
Discussion started by: Arun Mishra
7 Replies

3. Shell Programming and Scripting

Assigning numeric values to variable

I have a code like this v_num=9 comp_num=39 if then echo "pass" fi echo "end" I am getting an error ksh: v_num=99 comp_num=39 if then echo "pass" fi echo "end" (3 Replies)
Discussion started by: swayam123
3 Replies

4. Shell Programming and Scripting

String variable to numeric conversion in perl

Hi guys I am having this strange issue.Well my requirement is like below Compare two values between flat file and oracle DB Via perl script I am easily getting the rowcount Now I connect sql plus via perl and the column value that returns is string my $sqlplus_settings = ''; my... (7 Replies)
Discussion started by: Pratik4891
7 Replies

5. Shell Programming and Scripting

KSH: Test telnet and exit

Hi, I need to do a test Telnet in KSH and if the connection is good then disconnect the telnet session with out logging in and without exiting the shell script. Example output of a good connection: $telnet xxx.xx.xx.xxx xxxx Trying xxx.xx.xx.xxx... Connected to xxx.xx.xx.xxx. Escape... (1 Reply)
Discussion started by: calex
1 Replies

6. Shell Programming and Scripting

Remove non numeric values from a variable

Hello all, I am working on a basic script but need a little help. Issue: I am running a SQL Query using sqlplus and a shell script. I have the output of the statement stored as variable $A. $A is set to "other text here 45678754 other text here". I need to strip all text except that numeric... (13 Replies)
Discussion started by: ownedthawte
13 Replies

7. Shell Programming and Scripting

Replace variable length numeric string

I have a customer who logged some cc and bank account numbers in their apache logs. I got the cc numbers x'd out with sed -e 's/args=\{16\}/args=XXXXXXXXXXXXXXXX/g' -e 's/cardnum=\{16\}/cardnum=XXXXXXXXXXXXXXXX/g'but that wasn't too difficult due to the value being 16 digits. The bank account... (7 Replies)
Discussion started by: mk4mzid
7 Replies

8. Shell Programming and Scripting

to check variable if its non numeric

if test $b -ne then echo "\n\n\n\tPassword reset has been done successfully" else echo "\n\n\n\tAn error occurred" fi i want to check whether $b is non-numeric so how to do that? (3 Replies)
Discussion started by: sachin.gangadha
3 Replies

9. Shell Programming and Scripting

ksh - test if string contains alphanumeric...

Okay I will let users input spaces as well :) I am having a mental block. I have done a couple of searches but havent found anything that I understand (the likes of :alpha: and awk). Basically I want to give the user an option to enter some text which will go down as a field within a flat... (3 Replies)
Discussion started by: tugger
3 Replies

10. Shell Programming and Scripting

how to set a variable to accept alpha-numeric characters?

I am working on a shell program that needs to accept alpha-numeric input (i.e., P00375); when I use a simple 'read' statement to read in the input (i.e., read LOG), I receive the message "p00375: bad number". How must I adjust my input statement to accept alpha-numerics? Thanks! Brent (3 Replies)
Discussion started by: bcaunt
3 Replies
Login or Register to Ask a Question