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.

1#!/bin/bash◀
2THRESHOLD=${1:-80}
3USAGE=$(df / | awk 'NR==2 {print $5}' | tr -d '%')
4if [ "$USAGE" -gt "$THRESHOLD" ]; then
5 echo "ALERT: disk at ${USAGE}% (limit ${THRESHOLD}%)"
6 exit 1
7fi
8echo "OK: disk at ${USAGE}% (limit ${THRESHOLD}%)"
9exit 0

Variables

$1(empty)
terminal
$ ./disk-check.sh
Step 1 of 6The shebang line tells Linux to run this file with Bash. $1 holds the first word typed after the script name.

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_CONFIG is 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 pipefail makes 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 … done repeats 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.

6 steps
Press Play and the tutor will talk you through it while writing on the board. Pause any time to write on the board yourself.

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.

1#!/bin/bash◀
2LOG_DIR=/var/log/myapp
3DAYS=${1:-7}
4for f in $(find "$LOG_DIR" -name "*.log" -mtime +"$DAYS"); do
5 gzip "$f"
6 echo "compressed $f"
7done
8find "$LOG_DIR" -name "*.gz" -mtime +30 -delete
$ ./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
Step 1 of 13This script keeps a log folder from filling the disk. Press Next to run it one line at a time.

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.

Lab: arguments, exit codes and a backup

1. Run the disk check with no argument.

deploy@web-01:~/linux-automation-toolkit$

Check your understanding0 of 4 correct

Question 1

1.What does THRESHOLD=${1:-80} do?

Question 2

2.Why should a check script end with exit 1 when it finds a problem?

Question 3

3.What does find /var/log/myapp -name "*.log" -mtime +7 list?

Question 4

4.What does set -e do at the top of a script?

Key terms

Say the meaning to yourself, then tap a card to check.

Quiz score: 0 of 4

Tip: answer every quiz question correctly first.

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.