【问题标题】:How to goto an loop start while in if statement using bash?如何在使用 bash 的 if 语句中开始循环?
【发布时间】:2023-03-09 03:48:01
【问题描述】:

我们拥有的:

m=1
while [[ $m -le 5 ]]
do
curl --connect-timeout 5 --max-time 5 -X POST https://example.com/create >> test.txt
if [ -s test.txt ]
then
    echo "Empty file"
    ##"GO TO BEGIN OF LOOP"##
else
    echo "All fine"
fi

...

done

我们想检查test.txt 是否为空。如果是,我们需要转到循环的开头。

我是 stackoverflow 的新手。随时发表评论,以便我提高自己的提问方式。

【问题讨论】:

  • 使用continue ...
  • .. 并记住要有一个退出条件来中断,以避免无限循环
  • 非常感谢,continue 效果很好。

标签: bash loops if-statement


【解决方案1】:
m=1
while [[ $m -le 5 ]]
do
curl --connect-timeout 5 --max-time 5 -X POST https://example.com/create >> test.txt
if [ -s test.txt ]
then
    echo "Empty file."
    continue
else
    echo "All fine."
fi

...

done

【讨论】:

  • 虽然欢迎使用此代码 sn-p,并且可能会提供一些帮助,但它会是 greatly improved if it included an explanation of howwhy 这解决了问题。请记住,您正在为将来的读者回答问题,而不仅仅是现在提问的人!请edit您的答案添加解释,并说明适用的限制和假设。
猜你喜欢
  • 2014-01-15
  • 2017-03-06
  • 1970-01-01
  • 2014-01-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多