time_value=$(($large / 1000))
$large could be 60 or 57. I'm expecting 57/1000=0.057. But I'm getting 0. So, is there any way to do this?
time_value=$(($large / 1000))
$large could be 60 or 57. I'm expecting 57/1000=0.057. But I'm getting 0. So, is there any way to do this?
try
time_value=$((echo scale=3 ; echo $large / 1000) | bc )
where
scale=3 tell bc to use 3 digit after dot/commaecho $large / 1000 just compute divisionPlease note that, once you set floating point, you have to carry it all over the place.
if $time_value above is bellow 0, it cannot be used in usual $(( )) pattern.
.060. but later in my code im doing like thisflowspersec=$((flows / time_value))where flows could be the value 29. I'm getting errorsyntax error: operand expected (error token is ".060")– Veerendra K Oct 19 '15 at 08:21bcto add leading zero. Just use other tool likeawkto make calculation or even shell itself if you are using e.g.zsh. – jimmij Oct 19 '15 at 08:27awk. That would be very helpful – Veerendra K Oct 19 '15 at 08:32awk '{print $1/1000}' <<<$large. Useprintfinstead ofprintif you want to format it differently (e.g. round up). – jimmij Oct 19 '15 at 08:37awk '{printf $1/0.060}' <<<29that is my intention. It works fine, if I run in shell directly(awk '{printf $1/0.060}' <<<$testwheretest=29also works fine ). But notawk '{printf $1/$test2}' <<<29wheretest2=0.060– Veerendra K Oct 19 '15 at 09:55awkshell variable are not available, you need to pass them toawkwith -v option:awk -vT=$test2 '{printf $1/T}' <<<29orawk '{printf $1/$2}' <<<"29 $test2"- here$1stores first value, and$2second. – jimmij Oct 19 '15 at 10:31