![while 루프 아이디어](https://linux55.com/image/167720/while%20%EB%A3%A8%ED%94%84%20%EC%95%84%EC%9D%B4%EB%94%94%EC%96%B4.png)
n=0
while [ $n -le 10 ]
do
if sapcontrol -nr "$sid" -function GetSystemInstanceList | grep 'GREEN|YELLOW'
else
echo "SAP System successfully stopped!"
break
fi
done
상태가 노란색과 녹색이면 건너뛰고 회색으로 바뀔 때까지 기다렸다가 루프를 끊습니다. 3분 후 회색이 아닌 경우 오류 보고서가 나타납니다.
어떻게 해야할지 아이디어가 있나요? 감사해요!
답변1
아래 스크립트에는 필요한 모든 것이 있다고 생각합니다.
#!/usr/bin/env bash
# set defaults for used variables.
# variables could be set this way:
# var=foo var2=bar ./loopscript.sh
max_runtime_sec=${max_runtime_sec:-180}
recheck_time_sec=${recheck_time_sec:-5}
while true; do
# use pre-defined VAL or use default (output of sapcontrol)
# debugging: set VAL as commandlin arg and comment out the next line
VAL=$(sapcontrol -nr "$sid" -function GetSystemInstanceList | grep 'GREEN|YELLOW')
# switch-case with case-ignore Value of last command
case ${VAL,,} in
*green*|*yellow*)
if [[ $SECONDS > $max_runtime_sec ]]; then
echo "Error: max runtime of $max_runtime_sec seconds hit."
echo " Now breaking out of loop."
break
fi
# reduce system load and retry
sleep $recheck_time_sec
continue # do next loop iteration
;;
*gray*)
echo "Info: found VAL $VAL"
echo " Now breaking out of loop."
break
;;
*)
echo "Error: no valid VAL found. "
echo " stopping script now."
exit 1
;;
esac
done
echo "SAP System successfully stopped!"
다양한 값으로 출력 테스트:
$ time max_runtime_sec=4 VAL=green ./loop.sh
Error: max runtime of 4 seconds hit at Tue Feb 18 16:46:13 CET 2020. Script start at Tue Feb 18 16:46:08 CET 2020
Now breaking out of loop.
SAP System successfully stopped!
real 0m5,019s
user 0m0,013s
sys 0m0,004s
$ time max_runtime_sec=4 VAL=yellow ./loop.sh
Error: max runtime of 4 seconds hit at Tue Feb 18 16:46:25 CET 2020. Script start at Tue Feb 18 16:46:20 CET 2020
Now breaking out of loop.
SAP System successfully stopped!
real 0m5,016s
user 0m0,012s
sys 0m0,004s
$ time max_runtime_sec=4 VAL=grey ./loop.sh
Info: found VAL grey
Now breaking out of loop.
SAP System successfully stopped!
real 0m0,005s
user 0m0,005s
sys 0m0,001s
$ time max_runtime_sec=4 VAL=foo ./loop.sh
Error: no valid VAL found.
stopping script now.
real 0m0,007s
user 0m0,002s
sys 0m0,006s