I'm looking forward to extract disk memory usage from top command using grep command an save it into a variable available for the bash interpreter.
... and I assume you failed to grep those part from top
's output without understanding why...
This happens to many users, the reason is that the top
command has a somewhat unexpected output strategy. It does make sense, once you dig into it, but as said, unexpected:
By default, top
does not write it's output to stdout which is the precondition so that you can grep through it's output. However if you take a look into it's man page (man top
), then you will spot the -b
flag you can use to invoke it such that it does write it's output to stdout. This enables you to process the output as you are used to under unixoid environments, for example by "grepping" parts of the output for further usage.
Together with the -n
flag which allows you to limit the commands output to a single iteration (again: see the man page) you can construct a simple extraction command:
top -b -n 1|grep "KiB"
I think from here on you will find the way by yourself :-) Have fun!