Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash 如何在while循环中使用curl的值?_Bash_While Loop - Fatal编程技术网

Bash 如何在while循环中使用curl的值?

Bash 如何在while循环中使用curl的值?,bash,while-loop,Bash,While Loop,即使状态变为“向上”,而“待”也将继续。为什么我不能在循环中使用变量作为条件 export confup=false export blup=false timeout 30 bash <<EOF || false while [[ $( curl http://0.0.0.0/ | jq -r '.status') != "UP" ]]; do if [[ $( curl http://0.0.0.0/ | jq -r '.components."b

即使状态变为“向上”,而“待”也将继续。为什么我不能在循环中使用变量作为条件

export confup=false
export blup=false
timeout 30 bash <<EOF || false
while [[  $( curl http://0.0.0.0/ | jq -r '.status') != "UP" ]];
do
if [[ $( curl http://0.0.0.0/ | jq -r '.components."bridge.lock".status') == "UP" && \$blup == "false" ]]; then
    echo "true
    export blup=yes
fi
if [[ $( curl http://0.0.0.0/ | jq -r '.components."Configuration converted".status') == "UP" && \$confup == "false" ]]; then
    echo "true"
    export confup=yes
fi
echo $( curl "http://0.0.0.0/" | jq -r '.status')
sleep 5
done 
EOF
export confup=false
导出blup=false

超时30 bash您没有在循环中每次执行
curl
。它只执行一次,结果被替换到here文档中

您应该使用此处引用的文档,以便不在原始文档上执行变量和命令替换。然后您也不需要在
$blup
$confup
中转义
$

也不需要导出这些变量

timeout 30 bash <<'EOF' || false
while [[  $( curl http://0.0.0.0/ | jq -r '.status') != "UP" ]];
    do
    if [[ $( curl http://0.0.0.0/ | jq -r '.components."bridge.lock".status') == "UP" && $blup == "false" ]]; then
        echo "true"
        blup=yes
    fi
    if [[ $( curl http://0.0.0.0/ | jq -r '.components."Configuration converted".status') == "UP" && $confup == "false" ]]; then
        echo "true"
        confup=yes
    fi
    echo $( curl "http://0.0.0.0/" | jq -r '.status')
    sleep 5
done 
EOF

timeout 30 bash除了Barmbar给出的正确答案之外,我看不出你想要使用here DOC的好理由。它只是不必要地使事情复杂化,使代码更难维护。顺便说一句,由于您正在以子进程的形式运行here DOC,因此您对
blup
confup
的更改在(父)脚本中不可见。