在 shell 中比較 2 個回應代碼時遇到問題。運行 curl 并需要驗證回應是否在 200 到 400 之間。此外,當服務器關閉時,回應可能為“000”。
#!/bin/sh
response1="200" #curl first url
response2="000" #curl second url
if (( $response1 -ge 400 || $response1 -lt 200 || $response2 -ge 400 || $response2 -lt 200 )) ; then
echo "Something went wrong, response code is not in success range"
exit 1
else
echo "Success"
exit 0
fi
((: 200 -ge 400 || 200 -lt 200 || 000 -ge 400 || 000 -lt 200 :運算式中的語法錯誤(錯誤標記是“400 || 200 -lt 200 || 000 -ge 400 || 000 -lt 200")
如果我將括號更改為 [[...]],它總是回傳 true。如果我將 -lt 更改為 < 并將 -ge 更改為 >= 會出現以下錯誤:
((: 200 = 400 || 200 < 200 || 000 = 400 || 000 < 200 :嘗試賦值給非變數(錯誤標記是“= 400 || 200 < 200 || 000 = 400 || 000 < 200 ”)
uj5u.com熱心網友回復:
錯誤的操作員。正確的寫法是:
if (( response1 > 400 || response1 < 200 || response2 > 400 || response2 < 200 )) ; then
無需顯式取消對$, 的參考,只要確保您的變數僅包含整數即可。
uj5u.com熱心網友回復:
請嘗試:
if [ $response1 -ge 400 ] || [ $response1 -lt 200 ] || [ $response2 -ge 400
] || [ $response2 -lt 200 ] ; then
echo "Something went wrong, response code is not in success range"
exit 1
else
echo "Success"
exit 0
fi
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/369698.html
上一篇:如何在zsh中通過通配符除錯檔案來源中檔案中斷的位置
下一篇:TIB_Script運行大腳本
