Module 1 · Project 01 · Linux Server Automation Toolkit · Lesson 3 of 48
Scripts that decide: arguments, config and exit codes
Make scripts flexible with arguments, defaults, a config file and environment variables, and make them report success or failure.
In this lesson · about 30 minutes
- Read arguments with $1 and give them defaults
- Compare numbers with if [ … -gt … ] and return a meaningful exit code
- Load settings from a config file and environment variables
- Write a log-rotation and backup script
From "print everything" to "tell me if something is wrong"
The health check prints a lot, but someone still has to read it. A better script decides: it compares a number with a limit and says OK or ALERT. That needs four Bash ideas.
1. Variables. LIMIT=80 stores a value (no spaces around =). Use it with $LIMIT or ${LIMIT}.
2. Arguments. Words typed after the script name arrive as $1, $2, and so on; $# is how many there are. ${1:-80} means "use $1, or 80 if it is empty", a default.
3. Tests. if [ "$USAGE" -gt "$LIMIT" ]; then … fi. For whole numbers use -gt (greater than), -lt, -ge, -le, -eq, -ne. Always put variables in double quotes.
4. Exit codes. Every command ends with a number: 0 means success and anything else means failure. exit 1 sets it; $? reads the last one. Cron, CI pipelines and monitoring tools all trust this number, so a script that hides failures behind exit 0 is lying to them.
Try it · watch it run
Step through a disk-check script
Choose an argument and a disk level, then step through the script line by line. Try 'abc' to see a real bug that hides a failure.
Variables
| $1 | (empty) |
$ ./disk-check.sh
Settings in a config file
Hard-coding limits means editing code to change a setting. Put settings in a file and source it, so the script reads it as Bash variables:
# config/toolkit.conf
CPU_LIMIT=80
DISK_LIMIT=85
SERVICES="nginx ssh cron"
BACKUP_DIR=/srv/backups
#!/bin/bash
set -euo pipefail # stop on errors and unset variables
CONFIG="${TOOLKIT_CONFIG:-/opt/toolkit/config/toolkit.conf}"
source "$CONFIG"
for svc in $SERVICES; do
if ! systemctl is-active --quiet "$svc"; then
echo "ALERT: $svc is not running"
exit 1
fi
done
echo "OK: all services running"
TOOLKIT_CONFIGis an environment variable: a setting passed in from outside the script. Here it lets you point the script at a different config file for testing.set -euo pipefailmakes Bash stop at the first failing command (-e), treat an unset variable as an error (-u) and notice failures inside pipes. Put it near the top of every serious script.for … in … do … donerepeats for each word in the list.
Whiteboard · watch and try
Where does a script get its values?
Press Play. Three places a value can come from, and which one wins.
Tip: press Pause, then use the pen to work along on the board. Space plays and pauses; Ctrl+Z undoes your last mark.
Two more toolkit scripts: rotate logs and back up
Logs grow forever unless something trims them. The classic job: compress old logs and delete very old archives. find does the hard part: -name "*.log" matches by name and -mtime +7 means "last changed more than 7 days ago".
A backup script uses tar to pack a folder into one compressed file with the date in its name:
#!/bin/bash
set -euo pipefail
SRC=${1:-/var/www}
DEST=${BACKUP_DIR:-/srv/backups}
STAMP=$(date +%Y-%m-%d_%H%M)
mkdir -p "$DEST"
tar -czf "$DEST/www_$STAMP.tar.gz" "$SRC"
find "$DEST" -name "www_*.tar.gz" -mtime +14 -delete # keep two weeks
echo "Backup written: $DEST/www_$STAMP.tar.gz"
(Real Linux systems ship logrotate for logs. Writing your own once teaches you what it does, and the same pattern works for any cleanup job.)
Try it · watch it run
Watch a log-rotation script run
Set the age limit, then run the script and watch which files get compressed and which archives get deleted.
$ ./rotate-logs.sh 7
/var/log/myapp
- 📄 app.log0d · 48 MB
- 📄 app-11-13.log2d · 120 MB
- 📄 app-11-10.log5d · 135 MB
- 📄 app-11-06.log9d · 140 MB
- 📄 app-11-01.log14d · 152 MB
- 📄 app-10-20.log26d · 160 MB
- 🗜 app-10-12.log.gz34d · 16 MB
- 🗜 app-09-30.log.gz46d · 15 MB
Hands-on lab (simulated terminal)
Lab: arguments, exit codes and a backup
Practice with the toolkit's disk-check.sh and backup.sh. Disk usage on this server is 63%.
Step 1 of 6. Type each command and press Enter. Nothing runs on your computer.
1. Run the disk check with no argument.
Check your understanding0 of 4 correct
Key terms
Say the meaning to yourself, then tap a card to check.
Quiz score: 0 of 4
Written by Noviq Academy in its own words. The labs are simulations: no real servers are touched. When you build the projects yourself, use a practice virtual machine or a free-tier cloud account you control, watch your cloud spending, and never run cleanup or delete commands on a machine you care about. Tool names belong to their owners; versions and output are shown for learning and may differ slightly from yours. Read-aloud and the whiteboard voice use your device's built-in voices.