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)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.