Counter increment in Bash loop not working

前端 未结 13 2395
庸人自扰
庸人自扰 2020-12-02 05:31

I have the following simple script where I am running a loop and want to maintain a COUNTER. I am unable to figure out why the counter is not updating. Is it du

13条回答
  •  被撕碎了的回忆
    2020-12-02 06:14

    Instead of using a temporary file, you can avoid creating a subshell around the while loop by using process substitution.

    while ...
    do
       ...
    done < <(grep ...)
    

    By the way, you should be able to transform all that grep, grep, awk, awk, awk into a single awk.

    Starting with Bash 4.2, there is a lastpipe option that

    runs the last command of a pipeline in the current shell context. The lastpipe option has no effect if job control is enabled.

    bash -c 'echo foo | while read -r s; do c=3; done; echo "$c"'
    
    bash -c 'shopt -s lastpipe; echo foo | while read -r s; do c=3; done; echo "$c"'
    3
    

提交回复
热议问题