0
0
Bash Scriptingscripting~10 mins

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

Choose your learning style9 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.