bash:将结果分配给变量时不同的换行符计数 [英] bash: differing newline count when assigning result to variable

查看:18
本文介绍了bash:将结果分配给变量时不同的换行符计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我想查看一个程序的多少副本已经在运行.我可以这样做:

let's say I want to see how many copies of a program are already running. I could do something like this:

ps ax | grep -c "$0"

该命令本身会产生预期的结果.但是如果我试图将输出分配给一个变量,它会增加一!不管我怎么尝试:

that command by itself produces the expected result. BUT if I attempt to assign the output to a variable, it gets incremented by one! No matter how I try it:

var=$(ps ax | grep "$0" | sed -n '$=')
var=`ps ax | grep -c "$0"`

有人可以告诉我捕获正确输出的正确方法吗?

can someone please show me the right way to capture the correct output?

知道为什么会发生这种情况也很棒..

it would also be great to know why this is happening..

更新在@fedorqui 的第一个回复之后,我意识到我还不够清楚.让我详细说明一下:

UPDATE after the first response from @fedorqui I realize I wasn't clear enough. let me elaborate:

我在同一个 bash 脚本中运行上述所有三个命令.当我运行第一个时,它会打印出数字 2:程序本身和以该程序为参数的 grep 进程.当我在变量赋值中运行相同的命令时,会存储数字 3.

I am running all three commands above in the same bash script. When I run the first one, it prints out the number 2: the program itself and the grep process with that program as an argument. when I run those same commands within variable assignments, the number 3 is stored.

请注意,我使用了两种不同的计算行数的方法,grep 和 sed.在这两种情况下,它们都返回 3 而不是正确答案 2.

please note that I am using two different methods of counting lines, grep and sed. in both cases they return 3 instead of the correct answer, 2.

这是一个在 test.sh 文件中尝试的综合示例:

here is a consolidated example to try in a test.sh file:

echo -n "without assignment: "
ps ax | grep -c "$0"
var=$(ps ax | grep "$0" | sed -n '$=')
echo "using sed method: $var"
var=`ps ax | grep -c "$0"`
echo "using grep method: $var"

我的 debian 机器上的结果:

the results on my debian box:

without assignment: 2
using sed method: 3
using grep method: 3

再次提出问题:为什么会发生这种情况,以及如何预防或解决?

the questions again: why is this happening, and how to prevent or work around?

推荐答案

  • 命令替换本身在一个子shell中运行,所以这是一个bash进程

    您对 bash ($0) 的搜索,即 grep -c bash 也在当时的进程表中结束那是另一个包含字符串 bash 的进程 (grep).请注意,这可能不会在运行时显示在进程表中,具体取决于您的系统有多忙.

    your search for bash ($0) i.e. grep -c bash also ends up in the process table at that time so thats another process (grep) containing string bash. Note that, this might not show up in the process table at the time of running, depending on how busy your system is.

    你有两个(或其他)实际的 bash 进程(会话)正在运行,大概是剩下的

    And you have two (or whatever) actual bash processes (sessions) running presumably are the rest

    您可以使用 Regex 技巧来消除误报,即 grep 来自计数的一个:

    You can use a Regex trick to get rid of the false positive i.e. grep one from count:

    ps ax | grep -c "[b]ash"
    

    在执行命令替换时它仍然会计算子shell:

    It would still count the subshell while doing command substitution:

    var=$(ps ax | grep -c "[b]ash")
    

    因此您需要从该计数中手动删除一个.

    So you need to manually remove one from this count.

    示例:

    $ var=$(ps ax | grep -c "bash")    
    $ echo $var
    4
    
    $ var=$(ps ax | grep -c "[b]ash")   
    $ echo $var
    3
    

    这篇关于bash:将结果分配给变量时不同的换行符计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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