senthil senthil - 1 year ago 71
Bash Question

Arithmetic expression error in shell scripting

First i will take value of yesterday & Today file size in bytes (a=yesterday & b=today)

I need to calculate the percentage and find out if difference greater than or less than 10%

a=21007558 -- Yesterday
b=19330 -- Today


expr ((($b/$a) "*" 100) "-" 100) | bc

This is not working

Answer Source

you can use scale option as below;

c=$(echo "scale=6;((($b/$a) * 100) -100)" | bc)
echo $c

to lest than or greter than %10; you can try as below;

let c=$(echo $(printf %.$2f $(echo "scale=6;((($b/$a) * 100) -100)" | bc)))
if [[ c -lt -10  ]]; then
echo "decrease rate is %"$c    
if [[ c -gt 10  ]]; then
echo "increase rate is %"$c    
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download