Вопрос

I'm writing small geeklet for geektool, to alert me when sum of inactive and free RAM on my Mac will become slow. I'm not really good with bash, so I have a problem with final output (getting blank). Here is code:

inMem=$(top -l 1|awk '/PhysMem/ {print $6}'|sed s/M//) | freeMem=$(top -l 1|awk '/PhysMem/ {print $10}'|sed s/M//) | totalMem=$inMem+$freeMem | bc | echo $totalMem

Also wonder if my issue is optimal or not. Thanks a lot.

Это было полезно?

Решение

I wonder if this could actually simplify your commands. I can't test it since I'm not on OSX but I hope it works.

read inMem freeMem totalMem < <(top -l 1 | awk '/PhysMem/ { i = $6; sub(/M/, "", i); f = $10; sub(/M/, "", f); printf("%d %d %d\n", i, f, i + f); exit; }')
echo "inMem: $inMem"
echo "freeMem: $freeMem"
echo "totalMem: $totalMem"

Другие советы

Instead of parsing top, use the /proc/meminfo file. For example, with:

$ head -2 /proc/meminfo
MemTotal:        4061696 kB
MemFree:          335064 kB

you can see to total and free memory

user000001 answer is right, but then the question is "How to get /proc/meminfo output into variables?"

You can use this pure bash solution for parsing:

read -d '' _  memTotal _ _ memFree _ < <(head -2 /proc/meminfo)
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top