Chris Jones wrote:
> #!/usr/bin/gawk -f
> #-------------------------------------------------------------------------------

If you wish to be kind to people using random text editors then it
would be nice to shorten those marker comment lines by at least one
character so that they do not wrap around terminals.  Some editors put
a line wrap indicator in the last column indicating that the line
displayed is wrapped to the next line.

Also, as long as I am whining (:-) using eight columns for a single
indention level is alot of whitespace!  Of course this is personal
preference and a bike shed discussion but still...

> I have tried numerous things with while .. getfile .. restructuring the
> above, adding a BEGIN section .. and gotten nowhere.

Try my modification of your program.  I used getline to read the file.

Bob

#!/usr/bin/gawk -f
#------------------------------------------------------------------------------
# ./memswap :   display percentage of ram & swap currently in use
#
# usage     :   GNU/screen backtick
#
# notes     :   % ram used  = ((total ram  - used ram)  / total ram)  * 100
#           :   % swap used = ((total swap - used swap) / total swap) * 100
#------------------------------------------------------------------------------
BEGIN {
  while (getline < "/proc/meminfo") {
    if ($1=="MemTotal:")    {mt = $2};      # mt = total ram on system
    if ($1=="MemFree:")     {mf = $2};      # mf = free ram
    if ($1=="Buffers:")     {mb = $2};      # mb = ram used for buffers
    if ($1=="Cached:")      {mc = $2};      # mc = ram used for cache
    if ($1=="SwapTotal:")   {st = $2};      # st = total swap
    if ($1=="SwapFree:")    {sf = $2};      # sf = free swap
  }
  exit;
}
END {
  pmu = (mt-(mf+mb+mc)) * 100 / mt;         # pmu = % of ram  used
  psu = ((st-sf) * 100 / st);               # psu = % of swap used
  printf ("%2.1f %s %2.1f %s\n"), pmu, "%", psu, "%";
}


Reply via email to