📅  最后修改于: 2023-12-03 14:43:59.921000             🧑  作者: Mango
在Linux中,until
命令用于重复执行一个命令或一组命令,直到某个条件为真。
until condition
do
command
done
condition
:循环条件,只有在该条件为真时才会停止循环。command
:需要重复执行的命令或一组命令。#!/bin/bash
count=1
until [ $count -gt 10 ]
do
echo $count
count=$((count+1))
done
echo "Loop finished"
输出结果:
1
2
3
4
5
6
7
8
9
10
Loop finished
#!/bin/bash
service_name="httpd"
max_retry=10
current_retry=1
until [[ $(systemctl is-active $service_name) == "active" ]] || [[ $current_retry -gt $max_retry ]]
do
echo "$service_name is not active yet. Retry in 5 seconds..."
sleep 5
((current_retry++))
done
if [[ $(systemctl is-active $service_name) == "active" ]]
then
echo "$service_name is now active."
else
echo "Failed to start $service_name after $max_retry retries."
fi
说明:以上脚本将等待10次,每次等待5秒钟,以确保httpd
服务已启动。如果在最大尝试次数内服务仍未启动,则输出“Failed to start $service_name after $max_retry retries.”。
以上是Linux中until
命令的基本语法和示例代码。使用until
命令可以方便地实现重复执行一组命令的逻辑,直到某个条件为真。