bash 如何将unix top命令输出正确保存到变量中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2142377/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 21:35:14  来源:igfitidea点击:

How to correctly save the unix top command output into a variable?

linuxbashshellunixtop-command

提问by Cristian

I've got to save the output of the top command into a variable and I do this:

我必须将 top 命令的输出保存到一个变量中,然后执行以下操作:

myvar=`top -b -n1 | head -n 18`

The problem is that it seems to be ignoring the return characters, so when I echo the content of $myvarI see something like:

问题是它似乎忽略了返回字符,所以当我回显的内容时,$myvar我看到的是:

top - 15:15:38 up 745 days, 15:08, 5 users, load average: 0.22, 0.27, 0.32 Tasks: 133 total, 1 running, 132 sleeping, 0 stopped, 0 zombie Cpu(s): 6.4% us, 1.6%sy, 0.0% ni, 91.7% id, 0.3% wa, 0.0% hi, 0.0% si Mem: 2074716k total, 2038716k used, 36000k free, 84668k buffers Swap: 4192924k total, 107268k used, 4085656k etc...

top - 15:15:38 up 745 days, 15:08, 5 users, load average: 0.22, 0.27, 0.32 Tasks: 133 total, 1 running, 132 sleeping, 0 stopped, 0 zombie Cpu(s): 6.4% us, 1.6%sy, 0.0% ni, 91.7% id, 0.3% wa, 0.0% hi, 0.0% si Mem: 2074716k total, 2038716k used, 36000k free, 84668k buffers Swap: 4192924k total, 107268k used, 4085656k etc...

How can I save all top data correctly?

如何正确保存所有顶级数据?

回答by Greg Bacon

Notice the difference:

注意区别:

#! /bin/bash

x=`top -b -n 1 | head -n 5`
echo $x
echo --------------------
echo "$x"

Output:

输出:

top - 14:33:09 up 7 days, 5:58, 4 users, load average: 0.00, 0.00, 0.09 Tasks: 253 total, 2 running, 251 sleeping, 0 stopped, 0 zombie Cpu(s): 1.6%us, 0.4%sy, 70.3%ni, 27.6%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st Mem: 3926784k total, 3644624k used, 282160k free, 232696k buffers Swap: 9936160k total, 101156k used, 9835004k free, 1287352k cached
--------------------
top - 14:33:09 up 7 days,  5:58,  4 users,  load average: 0.00, 0.00, 0.09
Tasks: 253 total,   2 running, 251 sleeping,   0 stopped,   0 zombie
Cpu(s):  1.6%us,  0.4%sy, 70.3%ni, 27.6%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
Mem:   3926784k total,  3644624k used,   282160k free,   232696k buffers
Swap:  9936160k total,   101156k used,  9835004k free,  1287352k cached

Without the quotes, the contents of the variable are ground up in the shell's argument processing.

如果没有引号,变量的内容将在 shell 的参数处理中进行处理。

回答by Rob Wells

If you are looking for a particular piece of info within the top output i'd be inclined to filter the top output for what you're looking for before storing it rather than capture everything and then extract what you need.

如果您正在寻找顶部输出中的特定信息,我倾向于在存储之前过滤顶部输出以查找您要查找的内容,而不是捕获所有内容然后提取您需要的内容。

回答by Steve B.

You could pipe it out through sed to catch and transform the line breaks, e.g.

您可以通过 sed 将其输出以捕获并转换换行符,例如

top -n1 | sed 's/\(.*\)$/__CUSTOM_LINE_MARKER/g'

will output the CUSTOM_LINE_MARKER after every line. though Rob Wells answer above is probably a better approach.

将在每一行后输出 CUSTOM_LINE_MARKER。尽管 Rob Wells 上面的回答可能是更好的方法。