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??
# 8  
Old 04-09-2008
So my intuition was right there after all (-:

The thing with the "non-zero" case is I'd like to have only one place where you end up printing out a warning. I didn't want to bring it up before, but here's one attempt.

Code:
while true; do
  case $zipcode in
    00000) zipcode="force a warning on next iteration";;
    [0-9][0-9][0-9][0-9][0-9]) break;;
    *) echo "${myline}"
       echo "ZIPCODE WARNING! -> ${zipcode}" >&2
       break;;
  esac
done

The loop is really just to keep it going for one more round in the zero case. I honestly don't know if this is more elegant than two have two code branches where the warning is printed.
# 9  
Old 03-19-2009
test to see if variable is numeric

You can test the contents of a variable directly using ksh
pattern matching operators:

Code:
x=2763
if [[ $x == +([0-9]) ]]; then
        print okay
 else
        print naga
fi

This test only works with integers but since ksh only supports
integer arithmetic that's usually sufficient. For working with
real numbers, I usually run them through gawk.

(Pretend those underscores are blanks. I refuse to post unindented
code and this editor pushes everything up against the left margin.)

Last edited by vgersh99; 03-19-2009 at 08:19 PM.. Reason: trying to restore indentation (leading blanks) - use BB Codes
 
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