任何需要无限循环的编程语言的基本控制结构都是while循环。
while true ; do /path/to/script.sh ; if [ $? -ne 0 ] ; then continue ; else break ; fi ; done
Run Code Online (Sandbox Code Playgroud)
更具可读性的格式是:
while true
do
/path/to/script.sh # Ensure your script actually outputs exit status
if [ $? -ne 0 ] ; then
continue # if exit status not 0 ( not success ) , repeat
else
break # if successful - exit
fi
done
Run Code Online (Sandbox Code Playgroud)
当然,您需要确保您的脚本在成功时确实具有等于 0 的返回状态。您可能想也可能不想使用完整路径或./操作符在当前目录中运行脚本,或指定解释器,例如python /my/python/script.py
如有必要,您可以在下一次迭代开始之前添加延迟。为此,您可以在sleep 0.250之后fi但之前放置done