Here’s a bit of a puzzle - all of the clues in this post, and the answer will follow in the next one. It starts with trying to write a simple bash script to create an Azure Resource Group.

Working in my Ubuntu WSL2 environment, I wanted to write a bash shell script that created an Azure Resource Group if a group with that name doesn’t already exist.

The idiomatic way to test whether a resource group exists by using the Azure CLI tool:

$ az group exists --resource-group demogroup

This command literally returns either true or false, and is designed for use in a conditional test like this one:

# Create resource group
if [ $(az group exists -n demogroup) == false ]; then
    az group create -n demogroup --location eastus
else
    echo "resource group exists"
fi

Unfortunately, when I ran this script fragment, I was dutifully informed the resource group existed:

$ ./provision.sh
resource group exists

This in spite of the fact the resource group did not exist!

$ az group exists -n demogroup
false

I hope you can appreciate that this was quite confusing.

Skipping a whole bunch of failed attempts to solve the problem, I eventually modified the script to pull out the result of the az command and write it to the console before the test:

# Create resource group
EXISTS=(az group exists -n demogroup)
echo "\$EXISTS=--$EXISTS--"
if [ $EXISTS == false ]; then
    az group create -n demogroup --location eastus
else
    echo "resource group exists"
fi

Running this script, I got the following surprising output:

$ ./provision.sh
--XISTS=--false
resource group exists

This was the clue that I needed to solve the problem - can you solve it too?

Next Post
A bash puzzle, solved  02 Jul 2022
Prior Post
Improve your troubleshooting by aggregating errors  11 Jun 2022
Related Posts
Browsers and WSL  31 Mar 2024
Factory methods and functions  05 Mar 2023
Using Constructors  27 Feb 2023
An Inconvenient API  18 Feb 2023
Method Archetypes  11 Sep 2022
A bash puzzle, solved  02 Jul 2022
Improve your troubleshooting by aggregating errors  11 Jun 2022
Improve your troubleshooting by wrapping errors  28 May 2022
Keep your promises  14 May 2022
When are you done?  18 Apr 2022
Archives
June 2022
2022