UNIX GURU UNIVERSE
UNIX HOT TIP
Unix Tip 3316 - March 9, 2011
http://www.ugu.com/sui/ugu/show?tip.today
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
ARITHMETIC COMPARISON
In UNIX shell arithmetic comparison
is limited to integer values. Here
is a tip to compare floating values
using basic shell commands.
--------- CUT HERE-----------------
#! /bin/sh
# test shell script
n1="01.401"
n2="01.350"
function compareFloatSmall
{
sort -n <<: | head -1
$n1
$n2
:
}
function compareFloatGreat
{
sort -r -n <<: | head -1
$n1
$n2
:
}
small=$(compareFloatSmall $n1 $n2)
echo "Comparing $n1 to $n2: smaller $less"
great=$(compareFloatGreat $n1 $n2)
echo "Comparing $n1 to $n2: greater $great"
--------- CUT HERE-----------------
Alternatively you can use a small 'awk' program.
--------- CUT AGAIN HERE-----------
#! /bin/sh
# A couple of examples in awk.
n1="03.550"
n2="02.550"
echo "$n1 $n2" | awk '{
if ( $1 >= $2 ) print $1
if ( $1 <= $2 ) print $2
if ( $1 > $2 ) print $1
if ( $1 < $2 ) print $2
if ( $1 == $2 ) print $1, $2
}'
--------- CUT AGAIN HERE-----------
This tip generously supported by: sthackeray@netscape.net
--------------------------------------------------------------------------
To Subscribe: http://www.ugu.com/sui/ugu/show?tip.subscribe
To Unsubscribe: http://www.ugu.com/sui/ugu/show?tip.unsubscribe
To Submit A Tip: http://www.ugu.com/sui/ugu/show?tip.today
==========================================================================
DISCLAIMER: All UNIX HOT TIPS ARE OWNED BY THE UNIX GURU UNIVERSE AND ARE
NOT TO BE SOLD, PRINTED OR USED WITHOUT THE WRITTEN CONSENT OF THE UNIX
GURU UNIVERSE. ALL TIPS ARE "USE AT YOUR OWN RISK". UGU ADVISES THAT
ALL TIPS BE TESTED IN A NON-PRODUCTION DEVELOPMENT ENVIRONMENT FIRST.
Unix Guru Universe - www.ugu.com - tips@ugu.com - Copyright 1994-2001
==========================================================================
Tidak ada komentar:
Posting Komentar