David
David

Reputation: 1293

Azure CLI - check existence of frontend port with Bash

I'm using bash to check if an application gateway frontend port exists using the following code.

I'm new to bash and it's giving me some syntax errors. What is the create syntax?

line 79: =: command not found

line 80: [: ==: unary operator expected

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

Upvotes: 0

Views: 83

Answers (1)

mattst
mattst

Reputation: 13980

There are a number of errors in your posted Bash code.

$ is not used as a prefix when assigning a value to a variable and spaces are not allowed surrounding the assignment = operator.

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

== is incorrect syntax with single bracket conditionals.

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

To prevent word splitting it is generally a good idea to double quote variables. e.g.

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

Please check your scripts using ShellCheck before posting in future.

Upvotes: 1

Related Questions