Pro*_*iat 0 ubuntu awk shell-script proc ram
这是脚本:
TYPE="${BLOCK_INSTANCE:-mem}"
awk -v type=$TYPE '
/^MemTotal:/ {
mem_total=$2
}
/^MemFree:/ {
mem_free=$2
}
/^Buffers:/ {
mem_free+=$2
}
/^Cached:/ {
mem_free+=$2
}
/^SwapTotal:/ {
swap_total=$2
}
/^SwapFree:/ {
swap_free=$2
}
END {
if (type == "swap") {
free=swap_free/1024/1024
used=(swap_total-swap_free)/1024/1024
total=swap_total/1024/1024
} else {
free=mem_free/1024/1024
used=(mem_total-mem_free)/1024/1024
total=mem_total/1024/1024
}
pct=used/total*100
# full text
printf("%.1fG/%.1fG (%.f%)\n", used, total, pct)
# short text
printf("%.f%\n", pct)
# color
if (pct > 90) {
print("#FF0000\n")
} else if (pct > 80) {
print("#FFAE00\n")
} else if (pct > 70) {
print("#FFF600\n")
}
}
' /proc/meminfo
Run Code Online (Sandbox Code Playgroud)
这是我尝试运行时的错误:
$ ./memory
awk: run time error: not enough arguments passed to printf("%.1fG/%.1fG (%.f%)
")
FILENAME="/proc/meminfo" FNR=46 NR=46
1.1G/15.3G (7
Run Code Online (Sandbox Code Playgroud)
它打印了我想要的内容(内存使用情况),但也有错误。
任何人都可以帮忙吗?
Awkprintf将您的结尾%视为第四个格式说明符的开始。如果你要打印你需要一个文字%征兆%%,例如
$ awk 'BEGIN{printf("%.1fG/%.1fG (%.f%%)\n", 1.2, 3.4, 5.6)}'
1.2G/3.4G (6%)
Run Code Online (Sandbox Code Playgroud)