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/bashShebang. Tells the OS what interpreter to use.chmod +x script.shMake a script executableif [ condition ]; then ... fiConditional blockfor item in list; do ... doneFor loopwhile [ condition ]; do ... doneWhile loopfunction name() { ... }Define a reusable functioncommand > file.txtRedirect stdout to file (overwrite)command >> file.txtRedirect stdout (append)command1 | command2Pipe output to the next commandcrontab -eEdit your scheduled cron jobs- 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
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
Set up a cron job that runs your backup script every day at midnight.
- 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.