此命令有什么问题? [英] What is wrong with this command?

查看:127
本文介绍了此命令有什么问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我自己不是一个软件/脚本编写人,所以很难理解这里发生的事情:

I'm not a software/scripting folk myself so struggling to understand what is happening here:

watch -n 0.2 'ps -p $(pgrep -d',' -x snmpd) -o rss= | awk '{ i += $1 } END { print i }''

基本上,我想每秒打印5次snmp守护程序的Resident Set Size值(以确保分辨率).然后,我打算以此为基础,将输出重定向到文本文件以供以后分析,例如,我可以将数据放入图形中.

Basically I am wanting to print the Resident Set Size value of my snmp daemon 5 times a second (for fair resolution). I then intend on building from this to redirect the output to a text file for later analysis where I can put the data into a graph for instance.

我在这里遇到的麻烦是我可以运行以下命令:

The trouble I have here is that I can run the following fine:

watch -n 0.2 'ps -p $(pgrep -d',' -x snmpd) -o rss'

但是我只需要数字值,因此使用 awk 剥离所有内容,但该值很重要.

However I require just the numeric value only so using awk to strip out everything but that value is important.

运行上面的第一个命令会返回错误,我怀疑是由于 watch 处理单引号的方式,但是我不够聪明,无法理解它....

Running the first command above returns an error and I suspect due to the way watch is handling the single quotes, but I'm not smart enough to understand it....

有什么建议吗?

我也读过

pmap -x [pid]

也可以工作,但是当我分别使用 snmpd's 的PID运行它时,输出显然为零.也有任何想法吗?

works too, however when I run it with snmpd's respective PID the output is zero when clearly it is not. Any ideas on this too?

致谢.

推荐答案

如果引用的命令正确:

watch -n 0.2 'ps -p $(pgrep -d',' -x snmpd) -o rss= | awk '{ i += $1 } END { print i }''
             ^                ^ ^                         ^                           ^^
             1                0 1                         0                           10

单引号有问题. 1表示报价开始",0表示报价结束.以下命令行将为您工作:

You've got problems with your single quotes. The 1 indicates 'start of quote', the 0 indicates end of quote. The following command line should work for you:

watch -n 0.2 'ps -p $(pgrep -d"," -x snmpd) -o rss= | awk "{ i += $1 } END { print i }"'
             ^                                                                         ^
             1                                                                         0

双引号和$(...)也可以正常工作.单引号字符串作为整体发送到watch.以前,您有多个参数.

The double quotes and $(...) also work correctly. The single-quoted string is sent to watch as a whole. Previously, you had multiple arguments.

请注意,在工作命令中,您具有:

Note that in your working command, you have:

watch -n 0.2 'ps -p $(pgrep -d',' -x snmpd) -o rss'
             ^                ^ ^                 ^
             1                0 1                 0

现在,由于中间'01'之间的字符是逗号,而不是空格,因此Shell继续为watch赋予单个参数,但不包含引号. watch作为其第三个参数的含义是:

Now, because the character between the middle '01' is a comma, not a blank, the shell continues to give watch a single argument, but it doesn't contain the quotes. What watch gets as its third argument is:

ps -p $(pgrep -d, -xsnmpd) -o rss

在您的awk行中,1watch`获得多个参数:

With your awk-line, 1watch` gets multiple arguments:

ps -p $(pgrep -d, -x snmpd) -o rss= | awk {
i
+=
$1
}
END
{
print
i
}

它不知道如何处理多余的部分. (注意:$1的值将是外壳程序当前的$1(可能为空字符串,在这种情况下,将省略与$1相对应的参数.)

And it doesn't know what to do with the excess. (NB: The value of $1 would be the shell's current $1 (possibly an empty string, in which case the argument corresponding to $1 would be omitted.)

此变体在awk脚本中的$1之前带有反斜杠,似乎对我有用(当我寻找一个实际上正在运行的程序时-snmpd不在我测试的机器上运行,事情因此而瓦解了):

This variant, with a backslash before the $1 in the awk script, seemed to work for me (when I looked for a program which actually was running — snmpd was not running on the machine where I tested, and things fell apart because of that):

sh -c 'ps -p $(pgrep -d"," -x snmpd) -o rss= | awk "{ i += \$1 } END { print i }"'

如果您认为没有snmpd流程存在任何危险,那么您需要减少工作量.这就是我测试过的命令;您可以将watch -n 0.2代替sh -c.但是请注意, watch 的手册页确实明确指出:

If you think there's any danger that there is no snmpd process, then you need to do things a little less compactly. That's the command I tested; you can put the watch -n 0.2 in place of the sh -c. But note that the man page for watch does explicitly say:

请注意,将command赋予"sh -c",这意味着您可能需要使用额外的引号才能获得所需的效果.

Note that command is given to "sh -c" which means that you may need to use extra quoting to get the desired effect.

那是非常准确的!

如果您希望坚持使用单引号,则可以尝试:

If you prefer to stick with single quotes, you could try:

watch -n 0.2 'ps -p $(pgrep -d"," -x snmpd) -o rss= | awk '\''{ i += $1 } END { print i }'\'

'\''主题背后的想法是,第一个单引号终止当前的单引号字符串;反斜杠单引号会添加一个实际的单引号,最后一个单引号会开始一个新的单引号字符串.最后的'\'也可以写为'\''',但是最后两个单引号是多余的,因此我将其省略.

The idea behind the '\'' motif is that the first single quote terminates the current single-quoted string; the backslash single quote adds an actual single quote, and the last single quote starts a new single-quoted string. The '\' at the end could also be written '\''', but the last two single quotes are redundant, so I left them out.

这篇关于此命令有什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆