senthil senthil - 3 months ago 8
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

c=

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


This is not working

Answer

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    
fi
if [[ c -gt 10  ]]; then
echo "increase rate is %"$c    
fi
Comments