加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

在bash中模拟do-while循环

发布时间:2020-12-15 16:39:23 所属栏目:安全 来源:网络整理
导读:在bash中模拟do-while循环的最好方法是什么? 我可以在进入while循环之前检查条件,然后继续重新检查循环中的条件,但这是重复的代码。有更清洁的方法吗? 我的脚本的伪代码: while [ current_time = $cutoff ]; do check_if_file_present #do other stuffd
在bash中模拟do-while循环的最好方法是什么?

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

我的脚本的伪代码:

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

这不执行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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读