ksh,达到循环最大值时执行操作 [英] ksh, perform action when reaching loop maximal value

查看:74
本文介绍了ksh,达到循环最大值时执行操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望编写一个小的korn shell脚本,进行5个测试(每次测试之前要等待一段时间),然后,如果它们全部失败,则执行一个操作.

I am looking to write a small korn shell script doing 5 tests (by waiting some time before each one) and then, if they all fail, performing an action.

我当时正在做类似的事情:

I was looking at doing something like :

    for i in {1..5}
    do
       "doMyTest"             #it fills a variables "status" (but can unfortunately fails)
       if [ "$status" ]; then #if status is filled then we can leave the loop
          break
       fi
       sleep 3                #else wait some time before doing another try
    done

    if [ -z "$status" ]; then
       exit 1
    fi

... then the rest of my program

您是否知道我该如何更好地做到这一点? 听起来有点多余...

Do you have any idea how could I do this in a better way ? It sounds a bit redundant ...

非常感谢您.

推荐答案

您只想在所有测试失败时看到失败吗? 如果一个测试成功后可以跳过其他测试,则可以使用

You only want to see a failure when all tests fail? When you can skip the other tests when one test succeeds, you can use

test1 || sleep 3 && \
   test2 || sleep 3 && \
   test3 || sleep 3 && \
   test4 || sleep 3 && \
   test5 || exit 1

这篇关于ksh,达到循环最大值时执行操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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