r/bash • • 2d ago

help Need help getting proper RAM amount without root

I have a script free -g | grep Mem | cut -d " " -f 15 that outputs the amount of memory on the host system in GB. The two problems with it are 1. it only works on systems with over 10GB but less than 100GB, and 2. it outputs 30(GB) when in reality I have 32GB. sudo dmidecode is unfortunately not an option, as it requires root, and the script needs to be able to be run without it.

1 Upvotes

5 comments sorted by

1

u/kaptnblackbeard 1d ago edited 1d ago

Don't forget the system reserves some memory for itself. You need a command that lists the full amount of memory.

lsmem -b --summary=only | sed -ne '/online/s/.* //p'

This will give you total installed memory in bytes. You could remove the '-b' to get human readable format i.e. "30G" but you may run into similar range constraints (I don't know I haven't used it on a system with <1G).

1

u/The-Princess-Pinky 1d ago

I did this script a few years ago when working with Conky and lua

$ cat scripts/functions/Get_system_memory
#!/usr/bin/env bash
#############
# This is a function to get the total installed memory
# into the LUA scripts.
#
# The LUA script calls it like this:
# TotRam = conky_parse("${execi 86400 ~/scripts/functions/Get_system_memory}")
#
# /proc/meminfo reports MemTotal in kB.
# Convert kB to decimal GB and round to the nearest whole GB.
#
# This reports RAM using the same GB convention normally used
# by memory manufacturers (8 GB, 16 GB, 32 GB, etc.).
#
# Rounding also allows a substantial loss of recognized memory
# to be detected, such as a failed DIMM.
#
Memory=$(awk '/^MemTotal:/ {printf "%d", ($2 / 1000000) + 0.5}' /proc/meminfo)
echo "$Memory GB"
###############################################################
# NOTE: The value reported by /proc/meminfo may be lower than
# the physical RAM installed because of memory reserved
# by the system. Rounding compensates for the normal
# small difference while still exposing a significant
# loss of recognized memory.

0

u/Due-Celery4326 1d ago edited 1d ago

Essas duas questões surgem por razões diferentes:

  1. O comando cut -d " " -f 15 trata cada espaço como um delimitador. Como o free alinha suas colunas usando um número variável de espaços, este comando é pouco confiável ao lidar com diferentes quantidades de RAM.
  2. O comando free -g exibe a memória em GiB descartando casas decimais e considera apenas a memória utilizável pelo kernel, em vez da memória física total instalada.

Quantidade total de RAM em GiB, com duas casas decimais; awk '/MemTotal/ {printf "%.2f GiB\n", $2/1024/1024}' /proc/meminfo Quantidade de RAM arredondada para o GiB mais próximo awk '/MemTotal/ {printf "%.0f\n", $2/1024/1024}' /proc/meminfo

0

u/kcfmaguire1967 1d ago

Just using the (bash) shell's builtins

% while read -r key value _ ; do [[ $key == MemTotal: ]] && { print -r -- "$value"; break; }; done < /proc/meminfo

15226888

That KB, I think /proc/meminfo is always kb.

0

u/geirha 1d ago

This will give you the amount in KiB (even though it claims the values are in kB)

awk '/^MemTotal:/ {print $2}' /proc/meminfo

divide by 1024 a couple of times and you have the value in GiB.