tags:

views:

152

answers:

3

How can I get the total physical memory in bytes of my Linux PC?

I need to assign it to a bash script variable

Thanks in advance

A: 
grep MemTotal /proc/meminfo | awk '{print $2}'  

The returned number is in KB

Neuquino
it seems you have answered your own question. nevertheless, use one awk command to do the job. `awk '/MemTotal/{print $2}' /proc/meminfo`
ghostdog74
A: 

How about

var=$(free | awk '/^Mem:/{print $2}')
kiwicptn
no spaces between "=" sign when assigning variable. use `$()` syntax wherever possible.
ghostdog74
@ghostdog74: thanks (post fixed)
kiwicptn
grep _and_ awk?
Tadeusz A. Kadłubowski
@Tadeusz: thanks (post fixed)
kiwicptn
+1  A: 
phymem=$(awk -F":" '$1~/MemTotal/{print $2}' /proc/meminfo )

or using free

phymem=$(free|awk '/^Mem:/{print $2}')

or using shell

#!/bin/bash

while IFS=":" read -r a b
do
  case "$a" in
   MemTotal*) phymem="$b"
  esac
done <"/proc/meminfo"
echo $phymem
ghostdog74