首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使if语句正常工作,响应代码总是显示1,并且永远不会退出循环。

如何使if语句正常工作,响应代码总是显示1,并且永远不会退出循环。
EN

Unix & Linux用户
提问于 2021-06-10 21:54:23
回答 1查看 32关注 0票数 0

我试图检查一个应用程序是否已经重新启动成功,我没有问题停止应用程序,但有问题启动应用程序。该应用程序启动,但脚本继续运行,从来没有走出循环时,网站是备份。

我有一个var $aem_curl,它运行另一个脚本,如果CheckHttp OK: 200, found /crxde/ in 11038 bytes成功,它将显示以下结果,并给出0的响应代码。但如果失败,则显示CheckHttp CRITICAL: 5032的响应代码。

我的代码:

代码语言:javascript
运行
复制
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

我的产出:

代码语言:javascript
运行
复制
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: CRXDE LiteCRXDE Lite
EN

回答 1

Unix & Linux用户

回答已采纳

发布于 2021-06-11 04:59:35

尝试更像这样的东西:

代码语言:javascript
运行
复制
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 OR ./check-http-aem.rb返回退出代码0 (true)为止。

顺便说一句,我不知道您的$LOG_FILE变量是什么,也不知道为什么有时您要将文本排入其中,但另一次设置"$MESSAGE“。或者为什么不使用shell函数而不是变量。或者像logger这样的现有工具。这与这个问题并没有什么关系,所以我就假装它打印了一些东西给stderr,然后忽略它。

还有..。

最好使用数组来保存命令参数,而不是依靠shell在空格上拆分标量变量。例如,执行以下操作:

代码语言:javascript
运行
复制
aem_args=(-h localhost -P 4502 -p /crx/de/index.jsp --response-code 200 -q 'crxde' -t 3)
./check-http-aem.rb "${aem_args[@]}"

代码语言:javascript
运行
复制
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[@]}"

而不是:

代码语言:javascript
运行
复制
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变量,这可能会带来其他(可能是不必要的)后果。但是使用数组,这样做是很简单的。

换句话说,你可以这样做:

代码语言:javascript
运行
复制
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
票数 1
EN
页面原文内容由Unix & Linux提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://unix.stackexchange.com/questions/653759

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档