如何获取 Bash 中的总物理内存以将其分配给变量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2441046/
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
How to get the total physical memory in Bash to assign it to a variable?
提问by Neuquino
How can I get the total physical memory in bytes of my Linux PC?
如何获得 Linux PC 的总物理内存(以字节为单位)?
I need to assign it to a bash script variable.
我需要将它分配给一个 bash 脚本变量。
回答by Neuquino
grep MemTotal /proc/meminfo | awk '{print }'
The returned number is in KB
返回的数字以 KB 为单位
回答by ghostdog74
phymem=$(awk -F":" '~/MemTotal/{print }' /proc/meminfo )
or using free
或免费使用
phymem=$(free|awk '/^Mem:/{print }')
or using shell
或使用外壳
#!/bin/bash
while IFS=":" read -r a b
do
case "$a" in
MemTotal*) phymem="$b"
esac
done <"/proc/meminfo"
echo $phymem
回答by pschichtel
I came up with this one under the assumption, that the physical memory will be the first number in free's output:
我想出了这个假设,即物理内存将是 free 输出中的第一个数字:
free -m | grep -oP '\d+' | head -n 1
This allows you to configure free to output the unit you want (-m, -g, ...) and it is independent of the system language (other answers depend on the "Mem:" string in free's output which may change based on the language).
这允许您配置 free 以输出您想要的单位(-m,-g,...)并且它独立于系统语言(其他答案取决于 free 输出中的“Mem:”字符串,该字符串可能会根据语言)。
回答by kiwicptn
How about
怎么样
var=$(free | awk '/^Mem:/{print }')
回答by turtlemonvh
Silly inline python version, which looks overly complicated, but is actually kind of useful.
愚蠢的内联python版本,看起来过于复杂,但实际上很有用。
freemem=$(echo -e 'import re\nmatched=re.search(r"^MemTotal:\s+(\d+)",open("/proc/meminfo").read())\nprint(int(matched.groups()[0])/(1024.**2))' | python)
It returns the memory in GB.
它以 GB 为单位返回内存。
回答by Genius
I'll try to make this answer self explanatory, just keep up with me.
我会尽量让这个答案不言自明,只要跟上我的步伐。
To get the description of memory, you can use the free
utility :
要获取内存的描述,您可以使用该free
实用程序:
free -t
Output(in KB):
输出(以 KB 为单位):
total used free shared buff/cache available
Mem: 8035900 3785568 324984 643936 3925348 3301908
Swap: 3906556 271872 3634684
Total: 11942456 4057440 3959668
To extract all of these valuesfrom this output in a single column :
要从此输出中的单个列中提取所有这些值:
free -t | grep -oP '\d+'
Output(in KB):
输出(以 KB 为单位):
8035900
3866244
266928
650348
3902728
3214792
3906556
292608
3613948
11942456
4158852
3880876
Note: Minute difference can be there in values, which doesn't matter most of the times.
注意:值可能存在微小差异,这在大多数情况下并不重要。
If you just want to get the total
physical memory(mem+swap), it is the 10th value in above output :
如果您只想获取total
物理内存(mem+swap),则它是上述输出中的第 10 个值:
free -t | grep -oP '\d+' | sed '10!d'
Output(on my PC):
输出(在我的电脑上):
11942456
Note: All the above outputs are in
Kilo Bytes
. If you want inMega Bytes
orGiga Bytes
just append-m
or-g
after-t
in abovefree
commands respectively.For Example :
free -t -g | grep -oP '\d+' | sed '10!d'
Output(in Giga Bytes on my PC):
11
注意:以上所有输出都在
Kilo Bytes
. 如果你想分别在上面的命令中Mega Bytes
或Giga Bytes
只是追加-m
或-g
之后。-t
free
例如 :
free -t -g | grep -oP '\d+' | sed '10!d'
输出(在我的 PC 上以千兆字节为单位):
11