Operating System - HP-UX
1753865 Members
7714 Online
108809 Solutions
New Discussion

Keeping floating aspect in a sum

 
SOLVED
Go to solution
SwissKnife
Frequent Advisor

Keeping floating aspect in a sum

Hi,

 

Here my code, computing a sum like this:

   
    typeset -Z10 OCCUPANCY=0
    typeset -Z10 TOTALsize=0
    find /logiciel -name backup_logs -type d | while read folder
    do
        A=`du -ms $folder`
        OCCUPANCY=`echo $A | awk -F" " '{ print $1}'`
        echo "[$OCCUPANCY Mb] => $folder"
        (( TOTALsize=TOTALsize+OCCUPANCY ))
    done
    echo "[$TOTALsize Mb] => total size"
    

The output si something like this:

 

....
[0000000.64 Mb] => /logiciel/app/oracle/diag/rdbms/iotstd0/IOTSTD0/trace/cdmp_20170111084019/backup_logs
[0000000.65 Mb] => /logiciel/app/oracle/diag/rdbms/iotstd0/IOTSTD0/trace/cdmp_20170111094523/backup_logs
[0000000.65 Mb] => /logiciel/app/oracle/diag/rdbms/iotstd0/IOTSTD0/trace/cdmp_20170111104532/backup_logs
[0000000.64 Mb] => /logiciel/app/oracle/diag/rdbms/iotstd0/IOTSTD0/trace/cdmp_20170111131544/backup_logs
[0000000.06 Mb] => /logiciel/app/oracle/diag/rdbms/sianah00/SIANAH00/trace/backup_logs
[0000000.00 Mb] => /logiciel/app/oracle/diag/tnslsnr/AIXTEST3/listener/trace/backup_logs
[0000000.00 Mb] => /logiciel/app/oracle/product/11.2.0.3/dbhome_1/log/aixtest3/client/backup_logs
[0000000.00 Mb] => /logiciel/app/oracle/product/11.2.0.3/dbhome_1/network/log/backup_logs
[0000000.42 Mb] => /logiciel/app/oracle/product/11.2.0.3/dbhome_1/rdbms/audit/backup_logs
[0000000.00 Mb] => /logiciel/app/oracle/product/11.2.0.3/dbhome_1/rdbms/log/backup_logs
[0000000.00 Mb] => /logiciel/app/oracle/product/11.2.0.4/log/aixtest3/client/backup_logs
[0000000.00 Mb] => /logiciel/app/oracle/product/11.2.0.4/network/log/backup_logs
[0000001.18 Mb] => /logiciel/app/oracle/product/11.2.0.4/rdbms/audit/backup_logs
[0000000.00 Mb] => /logiciel/app/oracle/product/12.1.0/dbhome_1/network/log/backup_logs
[0000000.79 Mb] => /logiciel/app/oracle/product/12.1.0/dbhome_1/rdbms/audit/backup_logs
[0000000.04 Mb] => /logiciel/app/oracle/product/12.1.0/dbhome_1/rdbms/log/backup_logs


[0000000017 Mb] => total size

 

How to keep decimal aspect for the sum ?

 

Any ideas ?

Kind regards,

Den.  

 

 

   

 

 

3 REPLIES 3
Steven Schweda
Honored Contributor
Solution

Re: Keeping floating aspect in a sum

   I don't do arithmetic with fractions in a shell, so I know nothing,
but ...

   As I read the "man" pages, "typeset -Z" sets a field width, but says
nothing about what follows a decimal point, or even whether you have a
decimal point.  For example:

mba$ (( x = 2.50 + 3.50 ))
mba$ echo $x
6

   That is, "6", not "6.0", or "6.00", or "6.0000", or ...

   Adding "typeset -Z" does little to change this:

mba$ typeset -Z10 a
mba$ (( a = 2.50 + 3.50 ))
mba$ echo $a
0000000006

   If you want to format output, then "printf" may be closer to what you
want:

mba$ y=` printf '%010.2f' $x `
mba$ echo $y
0000006.00

No need for "typeset" this way.  Less ugly, but still aligned:

mba$ y=` printf '%10.2f' $x `
mba$ echo "$y"  
      6.00

Dennis Handly
Acclaimed Contributor

Re: Keeping floating point aspect in a sum

You can either do all the work in your awk program, including the sums.

Or you can do what COBOL does, use scaled arithmetic.  Which means you multiply your inputs by a power of 10 and when printing you do a divide and remainder to get the fractional parts.

SwissKnife
Frequent Advisor

Re: Keeping floating point aspect in a sum

Hi,

I finally used comething like this

    OCCUPANCY=$(echo "$B -$A" | bc)

 

Thanks folks for your help,

Kind regards, 

Den.