Skip to content
Advertisement

Unable to get correct output in linux script

#!/bin/bash
ac = "(free | grep Mem | awk '{print $4/$2 * 100.0}')"
echo "$ac"

I wanted to get the remaining(free) percentage of RAM usage as output storing in variable for further processing. Instead m getting $ac output as

    total        0.00

Please help

Advertisement

Answer

#!/bin/bash
ac="$(free | grep Mem | awk '{print $4/$2 * 100.0}')"
echo "$ac"

works fine for me.

(BTW, awk can do grep’s work too:

free | awk '$1=="Mem:" { print 100*$4/$2; }'

)

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement