Stop typing the same commands every morning. In this hands-on episode you'll learn Bash scripting from zero, then build a real tool step by step: health.sh, a server health check that tests disk space and services, takes arguments, counts problems, returns a proper exit code, runs as a normal command and reports every morning via cron. You'll learn the shebang, chmod +x, variables and quotes, arguments ($1, $#), exit codes ($?), if and the test operators, for and while loops, and how pros debug: bash -x, set -euo pipefail and shellcheck.
โฑ Chapters
0:00 Five commands every morning?
0:17 Your first script (shebang, chmod)
0:54 Variables & quotes
1:29 Arguments: $1, $#
1:51 Exit codes & if
2:29 The tests you'll use most
2:52 for & while loops
3:10 Project step 1: disk usage pipe
3:42 Project step 2: compare & test the bad case
4:08 Project step 3: check services
4:39 Project step 4: arguments & exit code
5:07 Debug: bash -x, set -euo pipefail, shellcheck
5:37 Make it a command + cron
5:57 5 beginner mistakes
6:21 Cheat sheet
6:39 Like & subscribe
6:51 Homework & up next
๐ The finished script (health.sh)
#!/bin/bash
LIMIT=${1:-80}
PROBLEMS=0
USED=$(df / --output=pcent | tail -1 | tr -dc "0-9")
if [ "$USED" -gt "$LIMIT" ]; then
echo "WARNING: disk / is ${USED}% full"
PROBLEMS=$((PROBLEMS + 1))
else
echo "OK: disk / is ${USED}% full"
fi
for SVC in ssh cron nginx; do
if systemctl is-active --quiet "$SVC"; then
echo "OK: $SVC is running"
else
echo "FAIL: $SVC is down"
PROBLEMS=$((PROBLEMS + 1))
fi
done
echo "Problems found: $PROBLEMS"
exit $PROBLEMS
โจ๏ธ Commands from this video
mkdir ~/scripts && cd ~/scripts a folder for your scripts
nano hello.sh create a script
chmod +x hello.sh make it executable
./hello.sh run it
NAME="srv01" a variable (no spaces around =)
TODAY=$(date +%F) store a command's output
echo $? exit code of the last command (0 = success)
[ -f file ] [ -d dir ] [ -z "$VAR" ] file exists / folder exists / empty variable
[ "$N" -gt 80 ] numbers: -eq -ne -lt -le -gt -ge
df / --output=pcent | tail -1 | tr -dc "0-9" disk usage of / as a plain number
systemctl is-active --quiet ssh is a service running? (exit code only)
./health.sh 30 run with an argument
bash -x health.sh 30 debug: show every line with real values
set -euo pipefail safe mode (put it under the shebang)
sudo apt install shellcheck && shellcheck health.sh find bugs
sudo cp health.sh /usr/local/bin/health use it as a normal command
crontab -e then: 0 8 * * * /usr/local/bin/health plus "two greater-than signs" and a log file daily report at 08:00
โ ๏ธ Compare numbers with -gt, never with the greater-than sign: in bash it writes to a file.
โ ๏ธ "bad interpreter ^M"? The script has Windows line endings: fix it with dos2unix.
๐บ Full course playlist:
โ Previous โ #13 Automate Tasks with cron:
โถ Next โ #15 Logs & Troubleshooting: coming soon
๐ฌ Homework: add a memory check (free -m) and a website check (curl) to health.sh, then post your script in the comments. I'd love to see it!
๐ If this helped, like & subscribe, it keeps the course free.
ยฉ netacademy.ge
#Linux #UbuntuServer #Bash