运行脚本10次或直到满足条件

smc*_*smc 4 shell bash shell-script for

我有以下shell脚本。

 OUTPUT=$(systemctl is-active etcd)
 if [[ $OUTPUT == active ]]; then
       echo "The result is successfull"
   else
       echo "The result is unsuccessfull"
 fi
Run Code Online (Sandbox Code Playgroud)

我想运行这个脚本 10 次,每次它会休眠 10 秒。我能够使用for i in {1..10}循环然后使用 sleep 命令来实现这一点。

for i in {1..10}; do
   sleep 10
   OUTPUT=$(systemctl is-active etcd)
   if [[ $OUTPUT == active ]]; then
       echo "The result is successfull"
   else
       echo "The result is unsuccessfull"
   fi
done
Run Code Online (Sandbox Code Playgroud)

但是如果脚本在(例如第一次或第二次等)迭代期间与条件匹配并且不想执行下一次迭代,我想中断脚本。

我想我需要实现 while 循环,但我不确定如何在那里添加条件和 for 循环。

jes*_*e_b 7

break 内置用于此。

for i in {1..10}; do
   sleep 10
   OUTPUT=$(systemctl is-active etcd)
   if [[ $OUTPUT == active ]]; then
       echo "The result is successful"
       break
   else
       echo "The result is unsuccessful"
   fi
done
Run Code Online (Sandbox Code Playgroud)