On a Ubuntu 24.04.1 LTS system with 32GiB of memory running the free command from the script produces the following result:
$ free -mh | awk '/^Mem:/{print $4}'
25Gi
Notice that the use of the h (or --human) option has causes the output to be reported in units of gibibyte. From man free:
-h, --human
Show all output fields automatically scaled to shortest three
digit unit and display the units of print out. Following units
are used.
B = bytes
Ki = kibibyte
Mi = mebibyte
Gi = gibibyte
Ti = tebibyte
Pi = pebibyte
The Gi suffix will lead to the integer expression expected error.
Therefore, remove the h option to get a value in mebibytes without a suffix. I.e.:
#!/bin/bash
THRESHOLD="500"
FREE_MEM=$(free -m | awk '/^Mem:/{print $4}')
if [ "$FREE_MEM" -lt "$THRESHOLD" ]; then
echo "insufficient storage. Available memory is ${FREE_MEM} MiB"
fi
Or if you want megabytes (power of 1000) rather than mebibytes (power of 1024) as your MB suggests, use free --mega rather than free -m.
#!/bin/bash -x), rerun script, and see what's being fed to the conditional; alternatively, after populating the variables addtypeset -p FREE_MEMto display the actual contents of variableFREE_MEM; regardless of the method used you should find thatFREE_MEMdoes not contain what you think it contains