Bash脚本破坏嵌套循环,“break [n]”失败

时间:2016-02-09 20:18:03

标签: bash infinite-loop break

这是我的代码:

while [[ "$counter" -gt 0 ]];
do
  startNewTest
  echo -e "\n" | ./myscript.sh send

  while :
  do
    sleep 30
    echo -e "\n Reading status... \n"
    ./myscript.sh status | \
    while read i
    do
      if echo $i | grep -q "$KEYWORD"
      then
        echo -e "\n Starting a new round of test... \n"
        break 2
      fi
    done
  done
  let counter=counter-1
done

当满足if条件时,“break 2”行应该打破2层循环,对吧?但是,当我运行脚本时,它只会破坏最内层的循环,并陷入无限的“while:”循环中。我在哪里弄错了? 我也试过“休息3”,也没用。

1 个答案:

答案 0 :(得分:1)

您可以使用myscript.sh对此进行测试吗? 看来你在管道标志后突破了一个子进程。 在没有子进程的情况下获取while循环中的输出可能会有所帮助:

while [[ "$counter" -gt 0 ]];
do
  startNewTest
  echo -e "\n" | ./myscript.sh send

  while :
  do
    sleep 30
    echo -e "\n Reading status... \n"

    while read i
    do
      if echo $i | grep -q "$KEYWORD"
      then
        echo -e "\n Starting a new round of test... \n"
        break 2
      fi
    done < <(./myscript.sh status)

  done
  let counter=counter-1
done