| 
     
       |  |  |  | 
            
              | 
             
               | 
               
                 |  | UGU: Unix Guru Universe - Unix Tip #4235- September 15, 2012
 -  Home : Help
 : Today's Tip Unix Tip #4235- September 15, 2012
 
 
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
 
 
 NOTE: All tips provided are USE AT YOUR OWN RISK. Tips are submitted
by various unix admins around the globe. UGU suggest you read and
test each tip in a non-volitile environment before placing into
production.
 
 LAST 5 TIPS
 4234
 - COPY A TREE WITH CPIO
 4233
 - AUTOMATICALLY SET DISPLAY
 4232
 - ANOTHER SORT BY FILE SIZE
 4231
 - FILE CONSISTANCY CHECK
 4230
 - LOCK DOWN TELNET OR FTP
 I want to 
 SUBSCRIBE
 and get a UGU Tip everyday.
 I want to 
 UNSUBSCRIBE
 and NOT get a UGU Tip everyday.
 If you have a UNIX TIP let us know, we just may use it:
(All tips become the property of the Unix Guru Universe)
 |  |  
                 |  |  
                 |   Copyright 1994-2024 Unix Guru Universe 
 |  |  |  |  |