gpt4 book ai didi

Bash 管道命令输出到总和循环

转载 作者:行者123 更新时间:2023-11-29 09:04:38 26 4
gpt4 key购买 nike

开始使用 bash,我喜欢它,但似乎有很多细微之处最终会在功能上产生很大的不同,诸如此类,无论如何,这是我的问题:

我知道这行得通:

total=0
for i in $(grep number some.txt | cut -d " " -f 1); do
(( total+=i ))
done

但为什么不呢?:

grep number some.txt | cut -d " " -f 1 | while read i; do (( total+=i )); done

一些.txt:

1 number
2 number
50 number

for 和 while 循环都分别接收 1、2 和 50,但是 for 循环显示 total 变量最后是 53,而在 while 循环代码中,它只是保持为零。我知道这里缺少一些基本知识,请帮助我。

例如,我也不知道管道的差异如果我跑

grep number some.txt | cut -d " " -f 1 | while read i; echo "-> $i"; done

我得到了预期的输出

-> 1
-> 2
-> 50

但是如果这样跑

while read i; echo "-> $i"; done <<< $(grep number some.txt | cut -d " " -f 1)

然后输出变为

-> 1 2 50

这对我来说似乎很奇怪,因为 grep 在不同的行中输出结果。好像这不是模棱两可的,如果我有一个文件在不同的行中只有数字 1 2 3,然后我运行

while read i; echo "-> $i"; done < someother.txt

然后输出将由 echo 在不同的行中打印,正如前面示例中所预期的那样。我知道 < 用于文件而 <<< 用于命令输出,但为什么存在行差异?

无论如何,我希望有人能对此事有所了解,谢谢您抽出宝贵时间!

最佳答案

grep number some.txt | cut -d " " -f 1 | while read i; do (( total+=i )); done

管道中的每个命令都在子 shell 中运行。这意味着当您将 while read 循环放入管道中时,任何变量赋值都会丢失。

参见:BashFAQ 024 - "I set variables in a loop that's in a pipeline. Why do they disappear after the loop terminates? Or, why can't I pipe data to read?"

while read i; do echo "-> $i"; done <<< "$(grep number some.txt | cut -d " " -f 1)"

要保留 grep 的换行符,请添加双引号。否则,$(...) 的结果会受到单词拆分的影响,这会将所有空格折叠成单个空格。

关于Bash 管道命令输出到总和循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28201306/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com