Scripting with variables and loops in Azure - Time & Space Complexity
When writing scripts in Azure, it is important to know how the time taken grows as you repeat tasks using loops and variables.
We want to understand how the number of repeated actions affects the total time to complete the script.
Analyze the time complexity of the following operation sequence.
for i in range(1, n + 1):
resource_name = f"resource-{i}"
az resource create --name "$resource_name" --resource-type "Microsoft.Storage/storageAccounts" --location eastus
This script creates n storage accounts in Azure, naming each uniquely using a variable inside a loop.
Identify the API calls, resource provisioning, data transfers that repeat.
- Primary operation: The command to create a storage account using Azure CLI.
- How many times: This command runs once for each value of i, so n times.
Each new storage account requires one create command, so the total commands grow directly with n.
| Input Size (n) | Approx. Api Calls/Operations |
|---|---|
| 10 | 10 create commands |
| 100 | 100 create commands |
| 1000 | 1000 create commands |
Pattern observation: The number of operations grows in a straight line as n increases.
Time Complexity: O(n)
This means the time to run the script grows directly in proportion to the number of resources you create.
[X] Wrong: "Running the create command inside a loop will take the same time no matter how many resources I create."
[OK] Correct: Each create command takes time and runs separately, so more resources mean more total time.
Understanding how loops affect execution time helps you write efficient scripts and explain your reasoning clearly in real-world cloud tasks.
"What if we created multiple resources in a single command instead of one per loop? How would the time complexity change?"
Practice
Solution
Step 1: Understand what variables do
Variables hold information like names or numbers that you want to use again without typing them multiple times.Step 2: Identify the correct purpose in scripting
Variables help scripts remember values so you can use them repeatedly and easily change them if needed.Final Answer:
To store values that can be reused throughout the script -> Option AQuick Check:
Variables = reusable values [OK]
- Confusing variables with loops
- Thinking variables delete resources
- Believing variables connect to internet
count with value 5 in an Azure CLI script?Solution
Step 1: Recall Azure CLI variable syntax
In Azure CLI scripts (bash style), variables are assigned without keywords usingname=valueformat.Step 2: Match the correct syntax
count=5 usescount=5, which is the correct bash style assignment.Final Answer:
count=5 -> Option DQuick Check:
Azure CLI variable = name=value [OK]
- Using 'var' or 'let' which are not bash syntax
- Using 'set' which is Windows CMD syntax
- Adding spaces around '=' which breaks assignment
for i in 1 2 3; do echo $((i * 2)) done
Solution
Step 1: Understand the loop iterations
The loop runs with i values 1, then 2, then 3.Step 2: Calculate the expression inside echo
Each iteration prints i multiplied by 2: 1*2=2, 2*2=4, 3*2=6.Final Answer:
2 4 6 -> Option AQuick Check:
Loop * 2 = 2 4 6 [OK]
- Confusing echo $i with echo $((i * 2))
- Expecting space-separated output on one line
- Thinking syntax error due to missing semicolons
for i in 1 2 3 do echo $i done
Solution
Step 1: Check for correct for loop syntax
In bash scripts, the for loop header must end with a semicolon or a newline before the do keyword.Step 2: Identify the missing semicolon or newline
The snippet has no semicolon or newline afterfor i in 1 2 3, causing syntax error.Final Answer:
Missing semicolon or newline after for statement -> Option CQuick Check:
For loop header needs semicolon/newline before do [OK]
- Forgetting semicolon after for statement
- Assuming $i is undefined
- Thinking echo syntax is wrong
rg1, rg2, and rg3 using a script with a loop and a variable prefix rg. Which script snippet correctly achieves this?Solution
Step 1: Understand variable concatenation in bash
To combine a variable and a number, use double quotes and place variables side by side like "$prefix$i".Step 2: Check each option for correct syntax
prefix=rg for i in 1 2 3; do az group create --name "$prefix$i" --location eastus done correctly uses "$prefix$i" to form names rg1, rg2, rg3. Others use invalid concatenation or miss quotes.Final Answer:
prefix=rg for i in 1 2 3; do az group create --name "$prefix$i" --location eastus done -> Option BQuick Check:
Use "$prefix$i" to join strings in bash [OK]
- Using + for string concat in bash
- Not quoting variable expansions
- Writing variable names without $ sign
Ready to prove it?
Pass the test and this pattern moves unknown
