我已经调试了一天,没有找到正确的问题,我注意到我的脚本没有在
sudo podman container inspect nova_libvirt | jq ".[].State.Status" | grep running
之后提前执行 if,因为它只显示其结果并在之后停止,我怀疑存在转义问题尝试了很多解决方案但没有成功
这是脚本
:“
#!/bin/bash
# Check if file does not exist
if [ ! -f "computes_to_delete" ]; then
echo "File 'computes_to_delete' not found"
exit 1
fi
while IFS= read -r compute_to_delete; do
compute_sh=${compute_to_delete}
{ cat <<EOF
#!/bin/bash
echo "Check VNFS Step!"
echo "The first compute to delete is $compute_to_delete"
server_name=$(cut -d'.' -f1 <<< "$compute_to_delete")
echo "server_name is : \$server_name"
desired_ip=\$(openstack server list -f value -c Networks -c Name | grep -i "\$server_name" | awk -F '[=,]' '{print \$2}')
echo "desired ip is : \$desired_ip"
if [ -z "\$desired_ip" ]; then
echo "Desired IP not found."
exit 1
fi
# Try to SSH into the server
ssh -o StrictHostKeyChecking=no heat@\$desired_ip '''
echo "Connected to $desired_ip."
pwd
echo \$PWD
sudo podman container inspect nova_libvirt | jq ".[].State.Status" | grep running
if [ $? -eq 0 ]; then
echo "Container nova_libvirt is running."
sudo podman exec nova_libvirt sh -c '
if virsh list --all | grep -q -E "^[[:space:]]*[0-9]+"; then
echo "Error: VMs are still running under nova_libvirt."
exit 1
else
echo "No VMs are running under nova_libvirt. You are all good to remove the compute after!"
exit 0
fi
else
echo "Container nova_libvirt is not running."
exit 1
fi ''' || { echo "Failed to connect to \$desired_ip." ; exit 1; }
EOF
} | sshpass -p ${PWD} ssh -o StrictHostKeyChecking=no stack@${DIRECTOR_IP} "source stackrc && bash -s" >> "check_vnfs.log"
done < "computes_to_delete"
日志文件显示:“
检查VNFS步骤!第一个要删除的计算是 cpu11.XX.exx server_name 是:cpu11 所需的 ip 是:11.11.11 连接到。 /home/heat /home/heat“运行”“
sh -x 显示:“
- IFS=
- 读取-r计算要删除
- compute_sh=cpu11.XX.exx
- 猫
- sshpass -p ecocenter ssh -o StrictHostKeyChecking=no stack@xx 'source /stackrc && bash -s' ++ 剪切-d。 -f1
警告:将“(我隐藏@”(ECDSA)永久添加到已知主机列表中。 bash: -c: 第 10 行:语法错误:意外的文件结尾
我尝试执行脚本,期望它前进并使用 sh 连接到容器以显示附加的项目,但它从未正确前进到这一点
'''
与 '
相同。
如果您一致地缩进代码,您会发现您的
if-else
和 sh -c
:
if [ $? -eq 0 ]; then
...
sudo podman ... sh -c '
if ...
else ...
fi
else
...
fi
' || ...
收盘价应出现在第一个
fi
之后,而不是第二个。
请注意,多个级别的扩展很容易出错(例如,
$?
可能应该是\$?
,如@konsolebox所述)。
您可以通过分成两部分来避免这种情况:
cat <<EOD1
# assign variables here
compute_to_delete=$compute_to_delete
EOD1
cat <<'EOD2'
# everything here is transmitted verbatim
EOD2