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

查看:114
本文介绍了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?

推荐答案

  • 命令替换本身在子外壳中运行,因此一个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天全站免登陆