bash - 在 Bash 中模拟 do-while 循环

标签 bash loops do-while

在 Bash 中模拟 do-while 循环的最佳方法是什么?

我可以在进入 while 循环之前检查条件,然后在循环中继续重新检查条件,但这是重复的代码。有更清洁的方法吗?

我的脚本的伪代码:

while [ current_time <= $cutoff ]; do
    check_if_file_present
    #do other stuff
done

如果在 $cutoff 时间之后启动,这不会执行 check_if_file_present,而 do-while 会执行。

最佳答案

两个简单的解决方案:

  1. 在 while 循环之前执行一次代码

    actions() {
       check_if_file_present
       # Do other stuff
    }
    
    actions #1st execution
    while [ current_time <= $cutoff ]; do
       actions # Loop execution
    done
    
  2. 或者:

    while : ; do
        actions
        [[ current_time <= $cutoff ]] || break
    done
    

关于bash - 在 Bash 中模拟 do-while 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16489809/

相关文章:

java - 在 Java 中执行 while 循环更改变量

c++ - (C++数组问题)我想通过在DoWhile过程中输入的数字代码打印订购的食物

c++ - 从 char* 到 int 的无效转换。 C++ 错误;

bash - wp-cli:无法在 MAMP 上运行 phpunit

bash - while循环后的左尖括号在bash中意味着什么?

bash - 如何在AWK中整理多个文件?

c - 遍历字符串并在 C 中用空格分隔输入

vb.net - 对于 ListBox1 中的每个项目做一些事情然后将项目添加到 listbox2 vb

Bash 脚本 : Printing a column with having space between the string

bash - bash 中的全局重命名