?
Solved

Korn Shell numeric comparison yielding unexpected results

Posted on 2009-02-13
7
Medium Priority
?
2,684 Views
Last Modified: 2013-12-26
Using Korn Shell, I need to be able to compare 2 numbers, possibly containing a decimal point to determine which number is larger than the other.

The following are my unexpected results, which I would

A) Like to understand why this behavior occurs
B) How to write the korn script to correctly report the larger vs smaller of the 2 numbers.

Treating variables as numeric
 
Test #1 - Passed
 
> if (( 20.50 < 20 )); then
> echo "20.50 is less then 20, wrong"
> fi
 
Test #2 - Failed
 
> if (( 20.50 <= 20 )); then
> echo "20.50 is less then 20, wrong"
> fi
20.50 is less then 20, wrong
 
 
 
Treating variables as string
 
Test #3 - Passed
 
> if [[ "20.50" -lt "20" ]]; then
> echo "20.50 is less then 20, wrong"
> fi
 
Test #4 - Failed
 
> if [[ "20.50" -le "20" ]]; then
> echo "20.50 is less then 20, wrong"
> fi
20.50 is less then 20, wrong

Open in new window

0
Comment
Question by:creas002
  • 4
  • 2
7 Comments
 

Author Comment

by:creas002
ID: 23632001
I found after RTM that Korn Shell 89 does not support decimals, but KSH 93 does.  To that point... So does AWK




#!/bin/ksh
 
f_compare ()
 
{
    
   v_larger_number=$(echo "$1 $2" | awk '{if ( $1 > $2 ) print $1}{if ( $2 > $1 ) print $2} {if ($1 == $2) print $1}')
 
}
 
f_compare 20.50 20
echo ${v_larger_number}
 
> ./compare_numbers.ksh 20.50 20
20.50

Open in new window

0
 
LVL 6

Accepted Solution

by:
peter991 earned 1500 total points
ID: 23633163
Some variants..
exp1 -eq exp2       True, if exp1 is equal to exp2.
exp1 -ne exp2       True, if exp1 is not equal to exp2.
exp1 -lt exp2       True, if exp1 is less than exp2.
exp1 -gt exp2       True, if exp1 is greater than exp2.
exp1 -le exp2       True, if exp1 is less than or  equal  to exp2.
exp1 -ge exp2       True, if exp1 is greater than  or  equal to exp2.
0
 
LVL 6

Expert Comment

by:peter991
ID: 23633219
If you want to compare numbers with decimals..



if test `echo "if ( $MAX_TEMP <= $TEMPLOCAL_C ) 0 ; if ( $MAX_TEMP > $TEMPLOCAL_C ) 1" | bc` = 0  
  then
...... etc

Open in new window

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 6

Expert Comment

by:peter991
ID: 23633223
#!/bin/sh there is..
0
 

Author Comment

by:creas002
ID: 23637098
I answered my own question and should have closed the question.  I will still award the points
0
 

Expert Comment

by:dtruth
ID: 26146977
I have the following code and I'm getting the errors below when I run the script.

"typeset:  -F: unknown option"

When I do a man on typeset, the F option is valid.  If I use "typeset -i a=2.0", then I get "2.0:  unexpected `.'".
Also, if there is a way to force the setting of the decimal value, will the if statement work as is?  I have a project that I'm working on, so it is urgent that I get past what should be a very simple problem, although I can't find good examples online without using awk.  While I like to use awk for stripping files, I'd like to avoid using it for mathematical functions.

Thanks in advance.



#!/bin/ksh

typeset -F2 a=2.0

x=240
y=1.25

a=`expr 1024 / ${x}`

if (( ${a} < ${y} ));
   then
         z=1
fi
0
 
LVL 6

Expert Comment

by:peter991
ID: 26197621
dtruth:
I think it's best if you start a new question.
More people will be able to help you.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Let's say you need to move the data of a file system from one partition to another. This generally involves dismounting the file system, backing it up to tapes, and restoring it to a new partition. You may also copy the file system from one place to…
Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
Suggested Courses
Course of the Month14 days, 14 hours left to enroll

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question