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:21bc
to add leading zero. Just use other tool likeawk
to 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
. Useprintf
instead ofprint
if you want to format it differently (e.g. round up). – jimmij Oct 19 '15 at 08:37awk '{printf $1/0.060}' <<<29
that is my intention. It works fine, if I run in shell directly(awk '{printf $1/0.060}' <<<$test
wheretest=29
also works fine ). But notawk '{printf $1/$test2}' <<<29
wheretest2=0.060
– Veerendra K Oct 19 '15 at 09:55awk
shell variable are not available, you need to pass them toawk
with -v option:awk -vT=$test2 '{printf $1/T}' <<<29
orawk '{printf $1/$2}' <<<"29 $test2"
- here$1
stores first value, and$2
second. – jimmij Oct 19 '15 at 10:31