我正在使用很棒的 wm 和 bashets 来制作一个小文本小部件来显示可用内存。我想将总千字节数转换为千兆字节(即 1.2)。这就是我想出的......
mem_Kb=$(grep -i memfree /proc/meminfo | cut -d: -f2 | tr -d [kB] | sed "s/^[ \t]*// ; s/[ \t]*$//")
mem_Gig=$(echo "scale = 1 ; $mem_Kb / 1000000" | bc )
echo mem_Gig: $mem_Gig
有哪些更好/更清洁的方法?
答案1
只需使用free
:
$ free -h
total used free shared buffers cached
Mem: 7.8G 6.8G 1.0G 0B 166M 4.2G
-/+ buffers/cache: 2.5G 5.2G
Swap: 7.8G 548K 7.8G
所以,就你而言:
$ mem_Gig=`free -h | awk '$2~/buf/{print $4}'`
$ echo $mem_Gig
5.2G
从man free
:
-h, --human
Show all output fields automatically scaled to
shortest three digit unit and display the
units of print out. Following units are used.
B = bytes
K = kilos
M = megas
G = gigas
T = teras
If unit is missing, and you have petabyte of
RAM or swap, the number is in terabytes and
columns might not be aligned with header.
--si Use power of 1000 not 1024.
所以,如果你想使用 1000,而不是 1024,你可以这样做:
$ mem_Gig=`free -h --si | awk '$2~/buf/{print $4}'`
$ echo $mem_Gig
5.5G