linux - Calculating rounded percentage in Shell Script without using "bc" -



linux - Calculating rounded percentage in Shell Script without using "bc" -

i'm trying calculate percentage of items in shell script. round off value, is, if result 59.5, should expect 60 , not 59.

item=30 total=70 percent=$((100*$item/$total)) echo $percent

this gives 42.

but actually, result 42.8 , round off 43. "bc" trick, there way without using "bc" ?

i'm not authorized install new packages. "dc" , "bc" not nowadays in system. should purely shell, cannot utilize perl or python scripts either

use awk (no bash-isms):

item=30 total=70 percent=$(awk "begin { pc=100*${item}/${total}; i=int(pc); print (pc-i<0.5)?i:i+1 }") echo $percent 43

linux shell percentage

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

c# - Create a Notification Object (Email or Page) At Run Time -- Dependency Injection or Factory -

Set Up Of Common Name Of SSL Certificate To Protect Plesk Panel -