Counter increment in Bash loop not working

前端 未结 13 2408
庸人自扰
庸人自扰 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:32

    COUNTER=$((COUNTER+1)) 
    

    is quite a clumsy construct in modern programming.

    (( COUNTER++ ))
    

    looks more "modern". You can also use

    let COUNTER++
    

    if you think that improves readability. Sometimes, Bash gives too many ways of doing things - Perl philosophy I suppose - when perhaps the Python "there is only one right way to do it" might be more appropriate. That's a debatable statement if ever there was one! Anyway, I would suggest the aim (in this case) is not just to increment a variable but (general rule) to also write code that someone else can understand and support. Conformity goes a long way to achieving that.

    HTH

提交回复
热议问题