Re: OT:korn shell and arithmetic

  • From: Mladen Gogala <gogala@xxxxxxxxxxxxx>
  • To: oracle-l@xxxxxxxxxxxxx
  • Date: Wed, 23 Jun 2004 01:43:17 -0400

On 06/22/2004 11:27:12 PM, Tim Gorman wrote:
> Sorry, my friend, but that doesn't work...
> 
>     $ if [ 1.2 > 1.0 ]; then
>     > echo "yes"
>     > else echo "no"
>     > fi
>     yes
> 
>     $ if [ 1.2 > 1.3 ]; then
>     > echo "yes"
>     > else echo "no"
>     > fi
>     yes
> 
> The following is rather annoying, but it seems to work...
> 
>     $ if [[ "`bc << __EOF__^J1.2 > 1.0^J__EOF__`" = "1" ]]
>     > then
>     > echo yes
>     > else echo no
>     > fi
>     yes
> 
>     $ if [[ "`bc << __EOF__^J1.2 > 1.3^J__EOF__`" = "1" ]]
>     > then
>     > echo yes
>     > else echo no
>     > fi
>     no
> 

Hey Tim, long time no see! It seems that "-gt" is the correct comparison 
operator
for the numerical values. Other then that, pdksh has only integer arithmetic 
implemented. Here is a revised version, without those pesky decimal points:

$ cat /tmp/ttt;/tmp/ttt
#!/bin/ksh
if [ 2 -gt 3 ]; then
echo "yes";
else echo "no";
fi;
if [ 3 -gt 2 ]; then
echo "yes";
else echo "no";
fi;
no
yes
$


My preferred shell (all right, it's an interpreter, not a shell) for that kind 
of tricks is perl.

It looks much nicer:
$ cat /tmp/ttt;/tmp/ttt
#!/usr/bin/perl
if (1.2 > 1.3) { print "yes\n"; }
else           { print "no\n"; }
if (1.3 > 1.2) { print "yes\n"; }
else           { print "no\n"; }
                                                                                
print "The sum is:", 1.2+1.3,"\n";
no
yes
The sum is:2.5
$



-- 
Mladen Gogala
Oracle DBA
----------------------------------------------------------------
Please see the official ORACLE-L FAQ: http://www.orafaq.com
----------------------------------------------------------------
To unsubscribe send email to:  oracle-l-request@xxxxxxxxxxxxx
put 'unsubscribe' in the subject line.
--
Archives are at //www.freelists.org/archives/oracle-l/
FAQ is at //www.freelists.org/help/fom-serve/cache/1.html
-----------------------------------------------------------------

Other related posts: