Skip to content
PHASE 09Intermediate2 weeks

Bash Scripting and Automation

Objective: Write Bash scripts that automate repetitive system tasks.

The Mission

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.

Programming fundamentals (language-agnostic)
  • 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.

Key Bash concepts
  • #!/bin/bashShebang. Tells the OS what interpreter to use.
  • chmod +x script.shMake a script executable
  • if [ condition ]; then ... fiConditional block
  • for item in list; do ... doneFor loop
  • while [ condition ]; do ... doneWhile loop
  • function name() { ... }Define a reusable function
  • command > file.txtRedirect stdout to file (overwrite)
  • command >> file.txtRedirect stdout (append)
  • command1 | command2Pipe output to the next command
  • crontab -eEdit your scheduled cron jobs
The Challenge4 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.

The Log
course-notes/phase-9-scripting/
  • bash-scripts/
    Subdirectory with all your scripts. Every script needs a comment block at the top: what it does, who wrote it, when. This is professional practice. Start now.
  • programming-concepts.md
    The 8 fundamentals above, explained in your own words with examples from your own scripts.
  • scripting-notes.md
    Concepts learned, mistakes made, error messages and how you resolved them.
  • cron-setup.md
    Your crontab entries and how you verified they run on schedule.
Resources
All phases