How to break out of a loop in Bash?

前端 未结 2 870
庸人自扰
庸人自扰 2020-12-04 14:08

I want to write a Bash script to process text, which might require a while loop.

For example, a while loop in C:

int done = 0;
while(1) {
  ...
  if         


        
相关标签:
2条回答
  • 2020-12-04 14:31
    while true ; do
        ...
        if [ something ]; then
            break
        fi
    done
    
    0 讨论(0)
  • 2020-12-04 14:37

    It's not that different in bash.

    workdone=0
    while : ; do
      ...
      if [ "$workdone" -ne 0 ]; then
          break
      fi
    done
    

    : is the no-op command; its exit status is always 0, so the loop runs until workdone is given a non-zero value.


    There are many ways you could set and test the value of workdone in order to exit the loop; the one I show above should work in any POSIX-compatible shell.

    0 讨论(0)
提交回复
热议问题