如何讓我的 if 語句正常運作,回應程式碼總是顯示 1 並且永遠不會脫離循環

如何讓我的 if 語句正常運作,回應程式碼總是顯示 1 並且永遠不會脫離循環

我正在嘗試檢查應用程式是否已成功重新啟動,停止應用程式沒有問題,但啟動應用程式時遇到問題。應用程式啟動,但腳本繼續運行,並且在網站備份時永遠不會退出循環。

我有一個 var ,它運行另一個腳本,如果成功並給出回應代碼,$aem_curl則顯示以下結果 。但如果失敗,它會顯示回應代碼CheckHttp OK: 200, found /crxde/ in 11038 bytes0CheckHttp CRITICAL: 5032

我的程式碼:

aem_curl="./check-http-aem.rb -h localhost -P 4502 -p /crx/de/index.jsp --response-code 200 -q 'crxde' -t 30"
STOP_TMOUT=15
echo "starting $AEM_APP this will take a few mins..." | ${LOG_FILE}
    sudo $restart_aem start
    count=0
    while true; do
      echo "Waiting for AEM to start try #${count} ..." | ${LOG_FILE}

        $aem_curl

     if [ $? -eq 0 ]; then
        echo "AEM has started! status code - $?" | ${LOG_FILE} && break
     else
        echo "AEM has not started yet - status code is $?" | ${LOG_FILE}
     fi
      if [ "$count" -eq "${STOP_TMOUT}" ]; then
              MESSAGE="Already waited 10 minutes for AEM start something is amiss." | ${LOG_FILE}
              exit 1
      fi
      count=$(($count+1))
      sleep 20
    done

我的輸出:

Waiting for AEM to start try #0 ...
CheckHttp CRITICAL: Request error: Failed to open TCP connection to localhost:4502 (Connection refused - connect(2) for "localhost" port 4502)
AEM has not started yet - status code is 1
Waiting for AEM to start try #1 ...
CheckHttp CRITICAL: 503
AEM has not started yet - status code is 1
Waiting for AEM to start try #2 ...
CheckHttp CRITICAL: 503
...
Waiting for AEM to start try #19 ...
CheckHttp CRITICAL: 200, did not find /'crxde'/ in 11038 bytes: <!DOCTYPE html><html><head><meta charset="utf-8"><meta http-equiv="X-UA-Compatible" content="IE=8"/><title>CRXDE Lite</title><link rel="shortcut icon" href="icons/crxde_favicon.ico"><link type="image/...
AEM has not started yet - status code is 1
Waiting for AEM to start try #20 ...
CheckHttp CRITICAL: 200, did not find /'crxde'/ in 11038 bytes: <!DOCTYPE html><html><head><meta charset="utf-8"><meta http-equiv="X-UA-Compatible" content="IE=8"/><title>CRXDE Lite</title><link rel="shortcut icon" href="icons/crxde_favicon.ico"><link type="image/...
AEM has not started yet - status code is 1

答案1

嘗試更多類似這樣的事情:

maxcount=15
count=0
while [ "$count" -lt "$maxcount" ] && ! ./check-http-aem.rb -h localhost -P 4502 -p /crx/de/index.jsp --response-code 200 -q 'crxde' -t 30; do
 ...
 sleep 20
 let count+=1
done

if [ "$count" -eq "$maxcount" ] ; then
  echo "AEM hasn't started" >/dev/stderr
  exit 1
fi

while 迴圈將運行直到 $count > $maxcount或者 ./check-http-aem.rb返回退出代碼 0(真)

順便說一句,我不知道你的$LOG_FILE變數是什麼,也不知道為什麼你有時會將文字輸入其中,但有時會設定「$MESSAGE」。或者為什麼不使用 shell 函數而不是變數。或現有的工具,例如logger.它與問題並不真正相關,所以我假裝它向 stderr 打印一些內容並忽略它。


也...

最好使用陣列來保存命令參數,而不是依賴 shell 在空格上分割標量變數。例如,執行下列操作:

aem_args=(-h localhost -P 4502 -p /crx/de/index.jsp --response-code 200 -q 'crxde' -t 3)
./check-http-aem.rb "${aem_args[@]}"

或者

aem_curl=./check-http-aem.rb
aem_args=(-h localhost -P 4502 -p /crx/de/index.jsp --response-code 200 -q 'crxde' -t 3)
$aem_curl "${aem_args[@]}"

代替:

aem_curl="./check-http-aem.rb -h localhost -P 4502 -p /crx/de/index.jsp --response-code 200 -q 'crxde' -t 30"
$aem_curl

這些本質上都做同樣的事情,但後者更容易犯錯並出現意想不到的行為。例如,您不能使用後一種形式輕鬆地將包含空格字元的參數傳遞給腳本,而不更改 IFS 變量,這將產生其他可能不必要的後果。但使用數組,做到這一點很簡單。

換句話說,你可以這樣做:

maxcount=15
count=0
aem_args=(-h localhost -P 4502 -p /crx/de/index.jsp --response-code 200 -q 'crxde' -t 3)

while [ "$count" -lt "$maxcount" ] && ! ./check-http-aem.rb "${aem_args[@]}"; do
 ...
 sleep 20
 let count+=1
done

相關內容