Bird
Raised Fist0
Bash Scriptingscripting~10 mins

Disk space monitoring script in Bash Scripting - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Disk space monitoring script
Start Script
↓
Check disk usage with df
↓
Parse output to get usage %
↓
Compare usage with threshold
↓
Send alert
↓
End Script
The script checks disk usage, compares it to a limit, and sends an alert if usage is too high.
Execution Sample
Bash Scripting
#!/bin/bash
THRESHOLD=80
USAGE=$(df / | tail -1 | awk '{print $5}' | sed 's/%//')
if [ $USAGE -gt $THRESHOLD ]; then
  echo "Disk usage is above threshold!"
fi
This script checks the root disk usage and prints a warning if usage exceeds 80%.
Execution Table
StepCommand/ActionValue/OutputConditionBranch Taken
1Set THRESHOLD=80THRESHOLD=80N/AN/A
2Run df / | tail -1 | awk '{print $5}' | sed 's/%//'USAGE=85N/AN/A
3Check if USAGE > THRESHOLD85 > 80TrueYes branch
4Print alert message"Disk usage is above threshold!"N/AN/A
5End scriptScript endsN/AN/A
💡 Script ends after printing alert because disk usage is above threshold.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
THRESHOLDunset80808080
USAGEunsetunset858585
Key Moments - 3 Insights
Why do we remove the % sign from the disk usage output?
The disk usage output includes a % sign (like '85%'), which is not a number. We remove it to compare usage as a number in the condition (see execution_table step 2).
What happens if disk usage is exactly equal to the threshold?
The condition uses 'greater than' (gt), so if usage equals threshold, the alert does NOT trigger (see execution_table step 3).
Why do we use 'tail -1' in the command?
'df' outputs a header line and then data lines. 'tail -1' gets the last line which contains the actual disk usage info (see execution_table step 2).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of USAGE after step 2?
A80
B75
C85
D90
💡 Hint
Check the 'Value/Output' column in step 2 of the execution_table.
At which step does the script decide to print the alert message?
AStep 2
BStep 4
CStep 3
DStep 5
💡 Hint
Look for the step where the action is 'Print alert message' in the execution_table.
If the disk usage was 75%, what branch would the script take at step 3?
ANo branch (do nothing)
BYes branch (print alert)
CError branch
DLoop back to step 2
💡 Hint
Refer to the 'Condition' and 'Branch Taken' columns in step 3 of the execution_table.
Concept Snapshot
Disk space monitoring script syntax:
- Use 'df' to get disk usage.
- Extract usage % and remove '%'.
- Compare usage number with threshold.
- If usage > threshold, send alert.
- Script ends after check.
Simple, effective disk monitoring in bash.
Full Transcript
This script starts by setting a threshold value for disk usage at 80%. It then runs a command to check the current disk usage of the root directory. The output includes a percentage sign, so the script removes it to get a pure number. Next, it compares this number to the threshold. If the usage is greater than 80%, the script prints a warning message. Otherwise, it does nothing and ends. This process helps monitor disk space and alert when usage is too high.

Practice

(1/5)
1. What is the main purpose of a disk space monitoring script in a server environment?
easy
A. To check disk usage and alert when space is low
B. To delete files automatically when disk is full
C. To speed up the disk read/write operations
D. To backup all files to another disk

Solution

  1. Step 1: Understand the script's goal

    The script's goal is to monitor disk usage to prevent running out of space.
  2. Step 2: Identify the correct purpose

    Among the options, only checking disk usage and alerting matches the monitoring function.
  3. Final Answer:

    To check disk usage and alert when space is low -> Option A
  4. Quick Check:

    Disk monitoring = alert on low space [OK]
Hint: Monitoring means checking and alerting, not deleting or backing up [OK]
Common Mistakes:
  • Confusing monitoring with automatic deletion
  • Thinking it speeds up disk operations
  • Assuming it performs backups
2. Which of the following is the correct syntax to get the disk usage percentage of the root partition using df and awk in bash?
easy
A. df -h / | awk '{print $5}'
B. df -h / | sed '2q;d'
C. df / | awk 'NR==1 {print $5}'
D. df -h / | awk 'NR==2 {print $5}'

Solution

  1. Step 1: Understand df -h / output

    The command shows disk usage for root; the second line (NR==2) has the data.
  2. Step 2: Use awk 'NR==2 {print $5}' to get usage percent

    This prints the 5th column (usage %) from the second line, which is correct.
  3. Final Answer:

    df -h / | awk 'NR==2 {print $5}' -> Option D
  4. Quick Check:

    Second line, fifth column = usage percent [OK]
Hint: Use NR==2 to skip header line in df output [OK]
Common Mistakes:
  • Printing all lines instead of second line
  • Using NR==1 which is header line
  • Using sed incorrectly to get the line
3. Given this script snippet:
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"
fi

What will be the output if the disk usage is 75%?
medium
A. Disk usage is normal
B. No output
C. Syntax error in script
D. Warning: Disk usage is above 80%

Solution

  1. Step 1: Extract usage value

    The script removes % sign and stores 75 in usage variable.
  2. Step 2: Compare usage with 80

    75 is not greater than 80, so the else branch runs.
  3. Final Answer:

    Disk usage is normal -> Option A
  4. Quick Check:

    75 <= 80 means normal usage [OK]
Hint: Compare numeric values after removing % sign [OK]
Common Mistakes:
  • Forgetting to remove % sign before comparison
  • Using string comparison instead of numeric
  • Misreading the if condition
4. Identify the error in this disk space check script snippet:
usage=$(df / | awk 'NR==2 {print $5}')
if [ $usage -gt 90 ]; then
  echo "Disk almost full"
fi

What problem will occur when running this script?
medium
A. No error, script runs fine
B. Syntax error due to missing quotes around $usage
C. df command will fail
D. awk command syntax error

Solution

  1. 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.
  2. Step 2: Identify missing % removal and quotes

    Also, % sign is not removed, causing numeric comparison error. Missing quotes cause syntax error.
  3. Final Answer:

    Syntax error due to missing quotes around $usage -> Option B
  4. Quick Check:

    Always quote variables in test to avoid syntax errors [OK]
Hint: Quote variables and remove % before numeric comparison [OK]
Common Mistakes:
  • Not quoting variables in if test
  • Not removing % sign before numeric test
  • Assuming df or awk commands fail
5. You want to create a bash script that checks all mounted partitions and sends an alert if any partition exceeds 85% usage. Which approach correctly implements this?
hard
A. Use du command on root directory and alert if usage exceeds 85%
B. Run df -h once and alert if total disk usage exceeds 85%
C. Use a loop over df -h output lines, extract usage %, compare with 85, and echo alert if exceeded
D. Check only root partition usage and ignore others

Solution

  1. Step 1: Understand requirement to check all partitions

    We must check each mounted partition's usage, not just total or root.
  2. Step 2: Loop through df -h output lines and compare usage

    Extract usage percent per line, compare with 85, and alert if exceeded.
  3. Final Answer:

    Use a loop over df -h output lines, extract usage %, compare with 85, and echo alert if exceeded -> Option C
  4. Quick Check:

    Loop and compare each partition usage [OK]
Hint: Loop through df output lines to check each partition [OK]
Common Mistakes:
  • Checking only total or root partition
  • Using du instead of df for disk usage
  • Not looping through all partitions
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard