2

I’m trying to find a way to round up a few numbers.

The topic I found: https://stackoverflow.com/questions/2395284/round-a-divided-number-in-bash

I am using the following command in my bash script..

free -h | gawk '/Mem:/{print $2}' | awk 'FNR == 1 {print $1 "B"}')

The following code will show me how much in total Memory i have installed. Currently, im getting 3.7GB. The problem is, i need this to be rounded up to 4GB.

I have a script that requests my machine a bunch of info and will echo it all out. (Prolly should use something different but haven't tried anything else yet as this is one of my projects that i am working on.

memory=$(free -h | gawk '/Mem:/{print $2}' | awk 'FNR == 1 {print $1 "B"}')

echo $memory

Right now it echo's out 3.7GB as mentioned below. I'v tried it different ways but can't seem to get it to 4GB.

3
  • 1
    This doesn’t address your rounding question, but see this question to correctly determine the amount of installed memory. Mar 21, 2018 at 11:58
  • Sadly can't install new software. I need to use the ones that come with Debian.
    – TheSebM8
    Mar 21, 2018 at 12:20
  • dmidecode is priority important so it should be present on most Debian systems. You can’t guarantee you’ll get the correct result from free (especially free -he.g. on systems with 10 GiB of RAM or more, it will lose a full gibibyte). Mar 21, 2018 at 12:30

3 Answers 3

4

You could do something like:

memory=$(
  LC_ALL=C free -h | awk '
    /^Mem/ {
      suffix = $2
      sub(/[0-9.]*/, "", suffix)
      printf "%.0f%sB\n", $2, suffix
    }'
)

(LC_ALL=C to make sure the numbers are printed using the . decimal radix (3.7G would be output as 3,7G in locales using comma as the decimal radix)).

On GNU/Linux systems, you can also do:

memory=$(
  awk '/^MemTotal/{print $2*1024}' < /proc/meminfo |
    numfmt --to=iec --format=%0f --suffix=B
)

Or:

memory=$(
  free -h | awk '/^Mem/{print $2}' |
    numfmt --from=iec --to=iec --format=%0f --suffix=B
)

(that one coping with locales where the decimal radix is not .).

Note that free on Linux reports that MemTotal field of /proc/meminfo. As per proc(5), that's the total usable RAM (i.e., physical RAM minus a few reserved bits and the kernel binary code). For the physical RAM, and for PCs, as pointed out by @StephenKit, you may be better off using dmidecode to get the information from the BIOS, though you'd need superuser privileges for that:

physical_memory=$(
  sudo dmidecode -t memory |
    awk '$1 == "Size:" && $2 ~ /^[0-9]+$/ {print $2$3}' |
    numfmt --from=iec --suffix=B |
    awk '{total += $1}; END {print total}' |
    numfmt --to=iec --suffix=B --format=%0f
)
11
  • Sadly this still gives me 3.7GB
    – TheSebM8
    Mar 21, 2018 at 12:19
  • @TheSebM8. How come? Does your awk print 3.7 for awk 'BEGIN{printf "%.0f\n", 3.7}'? Mar 21, 2018 at 12:20
  • @TheSebM8 You probably made a typo in that case.
    – Kusalananda
    Mar 21, 2018 at 12:21
  • @kusalanda no, i did notmake any typos. I copied what Stephane gave me. StéphaneChazelas that one does round it to 4.
    – TheSebM8
    Mar 21, 2018 at 12:23
  • @TheSebM8, is it possible that you get 3,7G instead of 3.7 (see edit)? Otherwise, like Kusalananda, I can't explain it other than with a typo. Mar 21, 2018 at 12:29
2
$ echo "3.7" | awk '{printf("%d\n",$1 + 0.5)}'
4
1
  • Can't use it like this. I need a way that does it for every different machine.
    – TheSebM8
    Mar 21, 2018 at 12:14
2

If you want to round to 2 decimal, here are some simple examples:

echo "12345.12345" | nawk '{printf ("%.2f\n", $1+0.005)}'
echo "12345.345" | nawk '{printf ("%.2f\n", $1+0.005)}'

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .