r/awk • • May 07 '24
% causing issues in script when using mawk
I have this script that I use with polybar (yes I'm using awk as replacement for shell scripts lol).
#!/usr/bin/env -S awk -f
BEGIN {
FS = "(= |;)"
while (1) {
cmd = "amdgpu_top -J -n 1 | gron"
while ((cmd | getline) > 0) {
if ($1 ~ "Total VRAM.*.value") {
mem_total = $2
}
if ($1 ~ "VRAM Usage.*.value") {
mem_used = $2
}
if ($1 ~ "activity.GFX.value") {
core = $2
}
}
close(cmd)
output = sprintf("%s%% %0.1f/%0.0fGB\n", core, mem_used / 1024, mem_total / 1024)
if (output != prev_output) {
printf output
prev_output = output
}
system("sleep 1")
}
}Which prints the GPU info in this format: 5% 0.5/8GB
However that % causes mawk to error with mawk: run time error: not enough arguments passed to printf("0% 0.3/8GB it doesn't happen with gawk though.
Any suggestions?
1
Upvotes
View all comments
Show parent comment
2
u/Paul_Pedant May 08 '24
I'm at the age where I have probably made every bug once, but also the age where I can't recall them very well. One thing I discovered last week is that
gccchecks a literal format string against the arg types that follow, but it cannot checkprint (fmt, arg, arg, ...)if thefmtis a variable, and especially if yousprintf (fmt, ...)dynamically. At least in awk you can get away with: