I'm trying to write a simple script that utilizes the `du` command, but I'm running into an issue with floating point math. I'm getting an error when I try to perform a calculation with a decimal, specifically: `arithmetic syntax error: invalid arithmetic operator (error token is ".6")`. However, when I run the same script directly in the terminal, it works fine. I'm using Zsh, but I've also tried running it with Bash. The problem line is:`echo " $((237 - $(cat $SC/tmp/du)))"`. Here's my complete script for context.
4 Answers
If you're only looking at file system usage, consider using `df /` or `df -BG /`. It directly gives you the numbers you need without any decimal issues.
Bash doesn't support floating point arithmetic directly. You should pipe the output to a tool like `bc` or `awk` instead. For example:
```bash
echo "237 - $(cat $SC/tmp/du)" | bc
``` Just make sure to format the command properly.
You should use `bc` for calculations involving floating points. Here's how it would look:
```bash
x=10
y=20
z=$(echo "$x + $y" | bc)
echo "z: $z"
``` That way, you can handle decimals without running into syntax errors.
If you're still facing issues after piping to `bc`, double-check your command. You might be inadvertently trying to perform the calculation in Bash itself. Instead, try this:
```bash
echo "237 - $(cat $SC/tmp/du)" | bc
``` This way, you're passing the whole string into `bc` for evaluation.

Related Questions
How To: Running Codex CLI on Windows with Azure OpenAI
Set Wordpress Featured Image Using Javascript
How To Fix PHP Random Being The Same
Why no WebP Support with Wordpress
Replace Wordpress Cron With Linux Cron
Customize Yoast Canonical URL Programmatically