模拟do-whil​​e循环在bash [英] emulating do-while loop in bash

查看:180
本文介绍了模拟do-whil​​e循环在bash的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是模拟在bash一个do-whil​​e循环的最佳方式?

What is the best way to emulate a do-while loop in bash?

我可以检查进入,而循环前的状态,然后再继续重新检查在循环中的条件,但是我们在重复code。是否有一个更清洁的方式?

I could check for the condition before entering the while loop, and then continue re-checking the condition in the loop, but that's duplicated code. Is there a cleaner way?

伪$ C $我的脚本的C:

Pseudo code of my script:

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

此不执行 check_if_file_ present 如果 $截止时间和做后推出助益,而会。

This doesn't perform check_if_file_present if launched after the $cutoff time, and a do-while would.

推荐答案

两个简单的解决方案

1)执行你的code while循环之前曾经

1.) execute your code once before the while loop

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

这篇关于模拟do-whil​​e循环在bash的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆