I need to extract the value for Free memory (MemFree) on my system, which used to work, but seems not to anymore.
cat /proc/meminfo
MemTotal: 3981600 kB
MemFree: 2126632 kB
MemAvailable: 2926732 kB
Buffers: 56716 kB
Cached: 862928 kB
I had a bash script, getFreeMem.sh that is used to do the job with extracting the value:
#!/bin/bash
var=$(cat /proc/meminfo | grep -oP '(?<=MemFree:).*')
echo $var
That is used in my OH-script:
val value = executeCommandLine(Duration.ofSeconds(5), "/home/openhabian/getFreeMem.sh")
var Number newValue = Integer::parseInt(transform("REGEX", ".*([0-9]+).*", value))
newValue = (newValue / 1000).intValue
logInfo("MemTest","value of initial command is: {}" + value)
logInfo("MemTest","value of command is: {}" + newValue)
OH_RPi_Memory_Free.postUpdate(newValue)
However now this gives a response with a “null” in front which I don’t understand, and I am unable to remove it.
2023-03-06 17:24:37.902 [INFO ] [rg.openhab.core.model.script.MemTest] - value of initial command is: null2122356 kB
2023-03-06 17:24:37.904 [INFO ] [rg.openhab.core.model.script.MemTest] - value of command is: null0
I have tried extracting the values using a different command, which didn’t work:
awk '/MemFree/ { print $2 }' /proc/meminfo
This removes the “KB” which is in the initial command, so I don’t need the REGEX, but still I have a “null” in front of the value, like with the first command.
The aim is to get a number that I can update my OH_RPi_Memory_Free (number) item with.
What is the formatting I am missing here??