问题
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