Round with division command

Hi I want to create a list of percentages but the shell script is bugging me.

If if I want to resolve the percentage of the quota used I get 0 because the schell script rounds by default.

Example

w1=860
w2=1024
w3=$((($w1/$w2)*100))

Echo $w3 gives 0

When I divide w1 by w2 the result is 0,83!

Can somebody help me?

w3=$( echo "scale=6; ( $w1 / $w2 ) * 100" | bc )

some scale

c=`echo "scale=6; $a / $b" | bc`

Hi when i use w3=$( echo "scale=6; ( $w1 / $w2 ) * 100" | bc )

I get the error Command not found

check whether the directory in which bc lives is in the $PATH

usually it should be as

/usr/bin/bc

bc is not located in the usr/bin folder. Is there an easy way to locate the command so I can put it in the path?

Could you please let us know the operating system that you are using ? :slight_smile:

Linux Version 2.4.20-28.5505.EMC
(gcc version 2.96 20000731 (Red Hat Linux 7.1 2.96-98))

Hi,

I quick search on the internet gives that on REDHAT 7.1 the bc package is not installed by default. This could be the cause that bc is not working for me. I am not allowed to install this software so I need an other way... can u help me?

Regards,

Michel

check hegemaro's post for division without using bc

or

w3=$( echo $w1 $w2 | awk ' { printf("%.2f", $1 / $2 * 100 ) } ' )

That works... thanks again!! you are a lifesaver

I was to quick with my response... I do have a minor problem.. I am getting the command not Found. This is the trace of the code

++ USERID=5003
echo $LINE | cut -f3 -d\|
+++ echo '|#5003' '|' '229194|1048576|1048576|' '|' '3081|' '0|' '0|' '|'
+++ cut -f3 '-d|'
++ USED= 229194
echo $LINE | cut -f4 -d\|
+++ echo '|#5003' '|' '229194|1048576|1048576|' '|' '3081|' '0|' '0|' '|'
+++ cut -f4 '-d|'
++ SOFT=1048576
echo $LINE | cut -f5 -d\|
+++ echo '|#5003' '|' '229194|1048576|1048576|' '|' '3081|' '0|' '0|' '|'
+++ cut -f5 '-d|'
++ HARD=1048576
$(echo $USED $HARD | awk '{printf ("%.1f", $USED/$HARD*100)}')
echo $USED $HARD | awk '{printf ("%.1f", $USED/$HARD*100)}'
++++ echo 229194 1048576
++++ awk '{printf ("%.1f", $USED/$HARD*100)}'
+++ 100.0
./quota.sh: 100.0: command not found
++ PERCENTAGE=
echo $USED $HARD | awk '{printf ("%.1f", $1/$2*100)}'

Thanks... I have everything working at the moment!