ksh, perform action when reaching loop maximal value

送分小仙女□ 提交于 2019-12-12 04:55:44

问题


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 ...

Thank you very much.


回答1:


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


来源:https://stackoverflow.com/questions/23785701/ksh-perform-action-when-reaching-loop-maximal-value

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!