在 Bash 中模拟 do-while 循环的最佳方法是什么?
我可以在进入 while
循环之前检查条件,然后在循环中继续重新检查条件,但这是重复的代码。有更清洁的方法吗?
我的脚本的伪代码:
while [ current_time <= $cutoff ]; do
check_if_file_present
#do other stuff
done
如果在 $cutoff
时间之后启动,这不会执行 check_if_file_present
,而 do-while 会执行。
最佳答案
两个简单的解决方案:
在 while 循环之前执行一次代码
actions() { check_if_file_present # Do other stuff } actions #1st execution while [ current_time <= $cutoff ]; do actions # Loop execution done
或者:
while : ; do actions [[ current_time <= $cutoff ]] || break done
关于bash - 在 Bash 中模拟 do-while 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16489809/