
다음 쉘 스크립트가 있습니다.
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