Write a script that alerts when any filesystem is above 80% usage.
BeginnerHands-onBash scripting tasks
Answer
#!/usr/bin/env bash
set -euo pipefail
THRESHOLD="${1:-80}"
df -P -x tmpfs -x devtmpfs | awk -v t="$THRESHOLD" 'NR>1 {
gsub("%","",$5)
if ($5+0 > t) printf "ALERT: %s mounted on %s is at %s%%\n", $1, $6, $5
}'
Key trick → df -P forces POSIX one-line-per-filesystem output, so long device names never wrap and break the column numbers.
More on Bash scripting tasks
Write the safe header you start every Bash script with, and explain each part.BeginnerParse an Nginx access log and print the top 10 client IPs by request count.BeginnerWrite a backup script that tars a directory with a timestamp and keeps only the last 7 backups.IntermediateWrite a health-check loop that polls a URL until it returns 200, with a timeout.IntermediateParse command-line flags `-e <env>`, `-v` (verbose) and `-h` in a Bash script.IntermediateUse `trap` to clean up a temp directory on exit, even on Ctrl+C or error.Intermediate