我有以下 shell 脚本。
OUTPUT=$(systemctl is-active etcd)
if [[ $OUTPUT == active ]]; then
echo "The result is successfull"
else
echo "The result is unsuccessfull"
fi
我想运行这个脚本 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
但如果脚本在(例如第一次或第二次等)迭代期间匹配条件,我想中断脚本,并且不想执行下一次迭代。
我想我需要实现 while 循环,但我不确定如何在那里添加条件和 for 循环。
答案1
这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