Script Valley
Linux & Bash for Developers
Advanced Bash & Automation/Assessment

Practice & Assessment

Test your understanding of Advanced Bash & Automation

Multiple Choice Questions

5
1

What does `set -o pipefail` do in a Bash script?

2

What does `*/15 * * * * /scripts/check.sh` run?

3

In Bash, how do you access all elements of an array while preserving elements that contain spaces?

4

What is the correct way to load environment variables from a .env file into a Bash script?

5

What does `flock -n 9` do in the context of script locking?

Coding Challenges

1
1

Multi-Environment Deployment Script

Write deploy.sh using getopts to accept: -e ENVIRONMENT (required, must be dev/staging/prod), -d for dry-run mode, -v for verbose. The script must: (1) Validate -e is provided and is one of the allowed values, exit with usage() if not, (2) Load config from a config/ENVIRONMENT.env file using source, failing clearly if the file is missing, (3) Use an indexed array of deployment steps (e.g. 'lint' 'test' 'build' 'sync'), loop through them printing each step, (4) In dry-run mode, print what would happen but skip actual rsync/ssh, (5) Use set -euo pipefail and a trap ERR handler, (6) Lock with flock to prevent concurrent runs. Test with ./deploy.sh -e dev -d. Time estimate: 30 minutes.

Hard

Mini Project

1

Automated Deployment & Monitoring Suite

Build a complete automation suite in ~/devops/ with four scripts that work together. (1) setup.sh: creates required directories, validates dependencies (curl, rsync, jq), sets up cron jobs for monitoring and cleanup. (2) deploy.sh: full production-grade script with getopts (-e env, -d dry-run, -b branch), reads .env config, validates environment, uses rsync with --dry-run confirmation, SSH post-deploy steps, flock locking, and trap ERR logging. (3) monitor.sh: checks disk, memory, CPU, and service health, appends JSON-formatted results to ~/devops/logs/health.json using a heredoc, exits 1 on threshold breach. (4) cleanup.sh: removes deploy logs older than 30 days, rotates health.json if over 10MB, and reports space recovered. Wire them together: setup.sh schedules monitor.sh every 5 minutes via crontab and cleanup.sh weekly. All scripts must use set -euo pipefail, functions, local variables, and shellcheck-clean code.

Hard