We help IT Professionals succeed at work.

# Korn Shell numeric comparison yielding unexpected results

on
Medium Priority
2,715 Views
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
``````
Comment
Watch Question

## View Solution Only

Commented:
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
``````
Commented:
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.

Not the solution you were looking for? Getting a personalized solution is easy.

Commented:
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
``````

Commented:
#!/bin/sh there is..

Commented:
I answered my own question and should have closed the question.  I will still award the points

Commented:
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.

#!/bin/ksh

typeset -F2 a=2.0

x=240
y=1.25

a=`expr 1024 / \${x}`

if (( \${a} < \${y} ));
then
z=1
fi

Commented:
dtruth:
I think it's best if you start a new question.
##### Thanks for using Experts Exchange.

• View three pieces of content (articles, solutions, posts, and videos)
• Ask the experts questions (counted toward content limit)
• Customize your dashboard and profile

OR

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the