Disk space monitoring script in Bash Scripting - Time & Space Complexity
We want to understand how the time to run a disk space monitoring script changes as the number of disks or partitions grows.
Specifically, how does the script's work increase when checking more disk entries?
Analyze the time complexity of the following code snippet.
#!/bin/bash
# Check disk usage for all mounted filesystems
for disk in $(df -h | tail -n +2 | awk '{print $1}'); do
usage=$(df -h | grep "^$disk" | awk '{print $5}' | sed 's/%//')
if [ "$usage" -gt 80 ]; then
echo "Warning: Disk $disk is $usage% full"
fi
done
This script loops through each disk or partition, checks its usage percentage, and warns if usage is above 80%.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping over each disk entry from the disk list.
- How many times: Once for each disk or partition found by
df. - Inside the loop: Running
dfandgrepcommands again for each disk.
As the number of disks (n) increases, the script runs the loop n times, with each iteration running df -h (O(n)) and grep (O(n)) over all n entries.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 loops × ~10 ops each = ~100 total |
| 100 | About 100 loops × ~100 ops each = ~10,000 total |
| 1000 | About 1000 loops × ~1000 ops each = ~1,000,000 total |
Pattern observation: The total work grows roughly in proportion to the square of the number of disks.
Time Complexity: O(n^2)
This means the script's running time grows quadratically as the number of disks increases.
[X] Wrong: "The script runs in constant time because it just checks disk usage once."
[OK] Correct: The script actually loops over each disk, so more disks mean more checks and longer run time.
Understanding how scripts scale with input size helps you write efficient monitoring tools and explain your code clearly in real work or interviews.
"What if we stored the output of df -h once before the loop instead of calling it inside the loop? How would the time complexity change?"
Practice
Solution
Step 1: Understand the script's goal
The script's goal is to monitor disk usage to prevent running out of space.Step 2: Identify the correct purpose
Among the options, only checking disk usage and alerting matches the monitoring function.Final Answer:
To check disk usage and alert when space is low -> Option AQuick Check:
Disk monitoring = alert on low space [OK]
- Confusing monitoring with automatic deletion
- Thinking it speeds up disk operations
- Assuming it performs backups
df and awk in bash?Solution
Step 1: Understand
The command shows disk usage for root; the second line (NR==2) has the data.df -h /outputStep 2: Use
This prints the 5th column (usage %) from the second line, which is correct.awk 'NR==2 {print $5}'to get usage percentFinal Answer:
df -h / | awk 'NR==2 {print $5}' -> Option DQuick Check:
Second line, fifth column = usage percent [OK]
- Printing all lines instead of second line
- Using NR==1 which is header line
- Using sed incorrectly to get the line
usage=$(df / | awk 'NR==2 {print $5}' | sed 's/%//')
if [ "$usage" -gt 80 ]; then
echo "Warning: Disk usage is above 80%"
else
echo "Disk usage is normal"
fiWhat will be the output if the disk usage is 75%?
Solution
Step 1: Extract usage value
The script removes % sign and stores 75 in usage variable.Step 2: Compare usage with 80
75 is not greater than 80, so the else branch runs.Final Answer:
Disk usage is normal -> Option AQuick Check:
75 <= 80 means normal usage [OK]
- Forgetting to remove % sign before comparison
- Using string comparison instead of numeric
- Misreading the if condition
usage=$(df / | awk 'NR==2 {print $5}')
if [ $usage -gt 90 ]; then
echo "Disk almost full"
fiWhat problem will occur when running this script?
Solution
Step 1: Check variable usage in test
The variable $usage contains a value like '85%'. Without quotes, test may fail if value has spaces or special chars.Step 2: Identify missing % removal and quotes
Also, % sign is not removed, causing numeric comparison error. Missing quotes cause syntax error.Final Answer:
Syntax error due to missing quotes around $usage -> Option BQuick Check:
Always quote variables in test to avoid syntax errors [OK]
- Not quoting variables in if test
- Not removing % sign before numeric test
- Assuming df or awk commands fail
Solution
Step 1: Understand requirement to check all partitions
We must check each mounted partition's usage, not just total or root.Step 2: Loop through
Extract usage percent per line, compare with 85, and alert if exceeded.df -houtput lines and compare usageFinal Answer:
Use a loop over df -h output lines, extract usage %, compare with 85, and echo alert if exceeded -> Option CQuick Check:
Loop and compare each partition usage [OK]
- Checking only total or root partition
- Using du instead of df for disk usage
- Not looping through all partitions
Ready to prove it?
Pass the test and this pattern moves unknown
