【问题标题】:sh shell script can't break the loopsh shell 脚本无法中断循环
【发布时间】:2013-08-22 13:13:54
【问题描述】:

sh 脚本在文件大小 = 0 时应该可以工作(如果它不下载文件,我手动检查了 stat,值是“0”)但它没有停止,所以我添加了带有 break 的“if”语句,仍然没有不工作,我怎么能打破它?请帮忙。

注意:可能还有其他方法可以自动下载 firefox,但我将 firefox 放在这里只是作为一个轻量级示例。

n=0
mm=22

while [ $n -eq 0 ]; do
  for a in $mm
  do
    for b in 0 1 2 3 4 5 6 7 8 9
    do
      for c in "" ".1" ".2"
      do
       wget -O ff.exe http://download.cdn.mozilla.net/pub/firefox/releases/latest/win32/ru/Firefox%20Setup%20$a.$b$c.exe
       n=$(stat -c %s ff.exe)
       echo "n = $n"
         if [ $n -ne 0 ]; then
          echo "n2 = $n"
          break
         fi
      done
    done
    mm=$(( $mm + 1 ))
  done
done

好的,我想通了:

n=0
mm=22

while [ $n -eq 0 ]; do
  for a in $mm
  do
    for b in 1 2 0 3 4 5 6 7 8 9
    do
      if [ $n -ne 0 ]; then
    break
      fi
      for c in "" ".1" ".2"
      do
    wget -O ff.exe http://download.cdn.mozilla.net/pub/firefox/releases/latest/win32/ru/Firefox%20Setup%20$a.0$c.exe
    n=$(stat -c %s ff.exe)
      if [ $n -ne 0 ]; then
        break
      fi
      done
    done
  mm=$(( $mm + 1 ))
  done
done

【问题讨论】:

  • 问题是什么?你是说break 应该导致脚本退出?

标签: linux loops terminal sh break


【解决方案1】:

你只是打破了最里面的for循环,如果你当时试图退出脚本,也许你应该使用exit

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-05-19
    • 1970-01-01
    • 2017-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多