繁体   English   中英

Azure CLI - 使用 Bash 检查是否存在前端端口

[英]Azure CLI - check existence of frontend port with Bash

我正在使用 bash 使用以下代码检查应用程序网关前端端口是否存在。

我是 bash 的新手,它给我一些语法错误。 什么是创建语法?

第 79 行:=:未找到命令

第 80 行:[: ==: 应为一元运算符

echo "Creating frontend port if it doesnt exist"
    $frontendportExists = $(az network application-gateway frontend-port list -g $resourceGroupName  
             --gateway-name $appGatewayName --query "[?name=='appGatewayFrontendPort'] | length(@)")
    if [ $frontendportExists == 0 ]; then
        az network application-gateway frontend-port create \
            --gateway-name $appGatewayName \
            --name appGatewayFrontendPort \
            --port 443 \
            --resource-group $resourceGroupName    
    fi

您发布的 Bash 代码中存在许多错误。

在为变量赋值时, $不用作前缀,赋值=运算符周围不允许有空格。

Wrong:
    $frontendportExists = $(az network...
Should be:
    frontendportExists=$(az network...

==是单括号条件句的不正确语法。

Wrong:
    if [ $frontendportExists == 0 ]; then
Replace with (Bash only):
    if [[ $frontendportExists == 0 ]]; then
Replace with (Posix):
    if [ "$frontendportExists" = "0" ]; then

为了防止分词,双引号变量通常是个好主意。 例如

az network application-gateway frontend-port create \
    --gateway-name "$appGatewayName" \
    --name appGatewayFrontendPort \
    --port 443 \
    --resource-group "$resourceGroupName"

请在以后发布之前使用ShellCheck检查您的脚本。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM