I think you want to use bc to calculate the percentage.

the lines

free_var=${free%.*};
total_var=${total%.*};

are removing everything after the decimal point, which leaves 2/2 * 100
which is of course 100%

I presume it does this so that bash can then perform the arithmetic
evaluation

per=$(($free_var*100/$total_var));

with only integers, since bash does not do floats

if you replace it with something like

per=`echo "($free/$total) * 100 | bc -l`

then you should be able to get the percentage.

Of course this is no longer pure bash, you now also need bc installed, but I
think it's pretty much everywhere by default.

On Tue, Oct 19, 2010 at 9:00 AM, DaZZa <[email protected]> wrote:

> Learned denizens
>
> $POE runs a system which is extensively Linux, and part of the system
> is a small script which monitors the free space various devices using
> some third party software.
>
> The software isn't the issue - doing the math to work out the
> percentage free is.
>
> We're inth e middle of installing a new system, and where the old
> system had disk space measured in gigabytes, this one returns values
> in terabytes - and therein lies the problem.
>
> The results from the third party software query are assigned as
> variables in the script and converted into round numbers as below
>
> free='/<external process>'
> total='<external process>'
>
> free_var=$(free%.*);
> total_var=$(total%.*);
>
> per=$(($free_var*100/$total_var));
>
> The "per" figure is the one I'm interested in - percentage free space.
>
> Now, with the system which returns gigabytes, this gives a good enough
> result from the first two variables to get close enough for the people
> who are managing the system, vis-a-vis
>
> web4:~ # echo $free
> 25.40G
> web4:~ # echo $total
> 61.14G
> web4:~ #
>
> Which gives a good enough result of 40% free.
>
> With the NEW system, the results are somewhat different
>
> web4:~ # echo $free
> 2.47T
> web4:~ # echo $total
> 2.70T
> web4:~ # echo $free_var
> 2
> web4:~ # echo $total_var
> 2
> web4:~ # echo $per
> 100
> web4:~ #
>
> Which gives a figure of 100% free - not a good thing.
>
> So, after this long and involved description, my question for those
> with much greater nouse than myself is - is there any way to take
> these operations
>
> free_var=${free%.*};
> total_var=${total%.*};
>
> so it returns 2.4 and 2.7 respectively instead of 2 and 2?
>
> Note that I didn't write the original script, so please, no comments
> of 'You should have done this" or "This way is better' - I'm not in a
> position to make wholesale changes to the script concerned to make it
> "better". I'm not modifying it at the moment - simply copying bits
> from the script and pasting them into another terminal window to get
> the output without changing the script itself.
>
> Any advice regarding changing the math appreciated.
>
> Thanks.
>
> DaZZa
> --
> SLUG - Sydney Linux User's Group Mailing List - http://slug.org.au/
> Subscription info and FAQs: http://slug.org.au/faq/mailinglists.html
>
-- 
SLUG - Sydney Linux User's Group Mailing List - http://slug.org.au/
Subscription info and FAQs: http://slug.org.au/faq/mailinglists.html

Reply via email to