![如何让我的 if 语句正常工作,响应代码始终显示 1 并且永远不会脱离循环](https://linux22.com/image/192179/%E5%A6%82%E4%BD%95%E8%AE%A9%E6%88%91%E7%9A%84%20if%20%E8%AF%AD%E5%8F%A5%E6%AD%A3%E5%B8%B8%E5%B7%A5%E4%BD%9C%EF%BC%8C%E5%93%8D%E5%BA%94%E4%BB%A3%E7%A0%81%E5%A7%8B%E7%BB%88%E6%98%BE%E7%A4%BA%201%20%E5%B9%B6%E4%B8%94%E6%B0%B8%E8%BF%9C%E4%B8%8D%E4%BC%9A%E8%84%B1%E7%A6%BB%E5%BE%AA%E7%8E%AF.png)
我正在尝试检查应用程序是否已成功重新启动,停止应用程序没有问题,但启动应用程序时遇到问题。应用程序启动,但脚本继续运行,并且在站点备份时永远不会退出循环。
我有一个 var ,它运行另一个脚本,如果成功并给出响应代码,$aem_curl
则显示以下结果 。但如果失败,它会显示响应代码CheckHttp OK: 200, found /crxde/ in 11038 bytes
0
CheckHttp CRITICAL: 503
2
我的代码:
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