Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Division through zero #4

Open
yvesh opened this issue Mar 19, 2020 · 2 comments
Open

Division through zero #4

yvesh opened this issue Mar 19, 2020 · 2 comments

Comments

@yvesh
Copy link

yvesh commented Mar 19, 2020

If you don't have any swap / your swap is zero byte you get an division by zero error ($2)

free -b | awk '$ 1 == "Swap:" {total=$ 2; used=$ 3; free=$ 4; pfree=($ 4*100/$ 2); pused=($ 3*100/$ 2); if("$1" == "") {printf("%.0f", free )} else {printf("%.0f", $1 "" )} }'

awk: cmd. line:1: (FILENAME=- FNR=3) fatal: division by zero attempted
free -h
              total        used        free      shared  buff/cache   available
Mem:            ....
Swap:            0B          0B          0B
@skblnx
Copy link

skblnx commented Aug 16, 2022

A bit late now, but might be helpful for someone else:
Quickfix: Use mawk if your machine has it. All my systems with mawk (mainly Debian Buster & Bullseye) execute this command fine, only the ones with GNU awk fail with the division by zero error.

@pocheoleg
Copy link

free -b | awk '$ 1 == "Swap:" {total=$ 2; used=$ 3; free=$ 4; if($ 2 == 0) {pfree=0; pused=0} else {pfree=($ 4*100/$ 2); pused=($ 3*100/$ 2)}; if("$1" == "") {printf("%.0f", free )} else {printf("%.0f", $1 "" )} }'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants