bash 要显示的 Shell 脚本 - 内存使用情况、磁盘使用情况和 CPU 负载?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24662641/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 10:49:57  来源:igfitidea点击:

Shell script to display - Memory usage, Disk Usage and CPU Load?

linuxbashshellunixawk

提问by AKIWEB

I want to diplay the Memory usage, Disk Usage and CPU Load in the following format:

我想以以下格式显示内存使用情况、磁盘使用情况和 CPU 负载:

Memory Usage: 33/512MB (6%)    
Disk usage: 4.2/20GB (23%) 
CPU Load: 0.01

So I started using the below script to do that but somehow it gives me an error

所以我开始使用下面的脚本来做到这一点,但不知何故它给了我一个错误

#!/bin/bash

free -m | awk 'NR==2{printf "Memory Usage: %s/%sMB (%.2f%)\n", ,,*100/ }'
df -h | awk '$NF=="/"{printf "Disk Usage: %d/%dGB (%s)\n", ,,}'
top -bn1 | grep load | awk '{printf "CPU Load: %.2f\n", $(NF-2)}' 

Below is the error I get -

以下是我得到的错误 -

awk: run time error: not enough arguments passed to printf("Memory Usage: %s/%sMB (%.2f%)
")
        FILENAME="-" FNR=2 NR=2

I am running ubuntu 12.04. Any idea what's wrong?

我正在运行 ubuntu 12.04。知道出了什么问题吗?

Update:-

更新:-

After running my below shell script -

运行我下面的 shell 脚本后 -

#!/bin/bash

free -m | awk 'NR==2{printf "Memory Usage: %s/%sMB (%.2f%%)\n", ,,*100/ }'
df -h | awk '$NF=="/"{printf "Disk Usage: %d/%dGB (%s)\n", ,,}'
top -bn1 | grep load | awk '{printf "CPU Load: %.2f\n", $(NF-2)}' 

I got below output -

我低于输出 -

Memory Usage: 128504/128896MB (99.70%)
Disk Usage: 3/48GB (8%)
CPU Load: 0.40

Does CPU Load looks ok? What does 0.40 means? And what number I should say will be high for CPU Load?

CPU 负载看起来正常吗?0.40 是什么意思?我应该说哪个数字对 CPU 负载来说会很高?

采纳答案by William Pursell

Try escaping the %:

尝试转义%

printf "Memory Usage: %s/%sMB (%.2f%%)\n"