Skip to content
PHASE 09 Intermediate 2 weeks

Bash Scripting and Automation

Objective: Write Bash scripts that automate repetitive system tasks.

If you're doing something more than twice, automate it. Scripting is what separates people who use computers from people who make computers work for them.

These programming fundamentals apply to Bash, Python, JavaScript, Go, and every other language you'll ever touch. Learn them once. Apply them everywhere.

  • Variables : Named containers that hold values. The name stays, the value can change.

  • Data types : Strings, integers, floats, booleans, lists.

  • Conditionals : If this, then do that. How programs make decisions.

  • Loops : Do something repeatedly. For each item in a list. While a condition is true.

  • Functions : Reusable blocks of code. Write once, call whenever you need it.

  • Input/Output : Reading data in (from a file, from a user), writing data out.

  • Error handling : Anticipate failures. Good code doesn't crash, it handles them.

  • Comments : The computer ignores them. Future-you needs them. Write them.

#!/bin/bash Shebang. Tells the OS what interpreter to use.
chmod +x script.sh Make a script executable
if [ condition ]; then ... fi Conditional block
for item in list; do ... done For loop
while [ condition ]; do ... done While loop
function name() { ... } Define a reusable function
command > file.txt Redirect stdout to file (overwrite)
command >> file.txt Redirect stdout (append)
command1 | command2 Pipe output to the next command
crontab -e Edit your scheduled cron jobs
4 tasks
  1. 1

    Write a Bash script that checks disk usage. If any partition is over 80% full, write a warning to /var/log/disk-warning.log with a timestamp.

  2. 2

    Write a Bash script that backs up /opt/project to /tmp/backups/ with a timestamp in the filename (e.g., project-backup-2026-04-21.tar.gz).

  3. 3

    Set up a cron job that runs your backup script every day at midnight.

  4. 4

    Write a Bash script that accepts a username as an argument and outputs whether that user exists, what groups they belong to, and when they last logged in.