gpt4 book ai didi

bash - 如何将 grep 重定向到 while 循环

转载 作者:行者123 更新时间:2023-11-29 09:40:03 27 4
gpt4 key购买 nike

您好,我有以下 bash 脚本代码

group2=0
while read -r line
do
popAll=$line | cut -d "{" -f2 | cut -d "}" -f1 | tr -cd "," | wc -c
if [[ $popAll = 0 ]]; then
group2 = $((group2+2));
else
group2 = $((group2+popAll+1));
fi
done << (grep -w "token" "$file")

我收到以下错误:

./parsingTrace: line 153: syntax error near unexpected token `('
./parsingTrace: line 153: `done << (grep -w "pop" "$file")'

我不想将 grep 传递给 while,因为我希望循环内的变量在外部可见

最佳答案

问题出在这一行:

done << (grep -w "token" "$file")
# ^^

你需要说 < 然后 <() 。第一个是表示 while 循环的输入,第二个是 process substitution 的输入:

done < <(grep -w "token" "$file")
# ^ ^

但是请注意,您还需要检查许多其他事项。请参阅评论进行讨论并将代码粘贴到 ShellCheck 中以获取更多详细信息。此外,通过指明一些样本输入和所需的输出,我相信我们可以找到更好的方法来做到这一点。

关于bash - 如何将 grep 重定向到 while 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31878919/

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