user3299633 - 10 months ago 79

Linux Question

Can someone assist with my handling this calculation that I need in bash?

Currently I have the following:

`Size=$((IPS * DataPages / (1024 * 1024 * 1024) * 1.05))`

But I get the following error when trying to execute:

`./buffer: line 20: IPS * DataPages / (1024 * 1024 * 1024) * 1.05: syntax error: invalid arithmetic operator (error token is ".05")`

I'm open to using other alternatives, like either print or awk or even bc, but I need some guidance.

Answer

`bash`

does not support floating point arithmetic. Use `bc`

for that. To enabled floating point arithmetic in `bc`

pass the `--mathlib`

(or `-l`

) option:

```
IPS=2
DataPages=3
bc --mathlib <<< "($IPS * $DataPages / (1024 * 1024 * 1024) * 1.05)"
```

To capture that into a shell variable use process substitution:

```
...
Size=$(bc --mathlib <<< "($IPS * $DataPages / (1024 * 1024 * 1024) * 1.05)")
```