Objective. Students will add input validation, exit codes, and cron scheduling to Bash scripts and verify correct behavior under failure conditions.
This lesson, its worked examples and its practice questions has not yet been verified by an industry-certified professional or by a certification or approval body. It is a study draft and may contain errors. Do not use it as instructions for work on a job site or on live equipment. Always follow your instructor, your supervising professional, and current official references and regulations.
VoTechWorks program approval and accreditation are still in progress. Credentials are issued only by the certifying bodies named in each program, after their own exams.
A script that works only under perfect conditions is not production-ready. Real servers encounter missing files, wrong arguments, occupied ports, and permission errors every day. A professional Bash script must detect those problems, report them clearly, and exit with a code that other tools can act on. This lesson adds three layers of reliability to your scripts: strict-mode settings, input validation with exit codes, and automated scheduling via cron.
Place this line near the top of every script you write in this program:
```bash
set -euo pipefail
```
Without these options, a script can sail past a failed command and continue doing damage. Together they act like a safety net that stops execution as soon as something goes wrong.
Users and automated callers will sometimes invoke your script incorrectly. Check required arguments at the top of the script and print helpful guidance when they are missing.
```bash
usage() {
echo "Usage: $0 <source_dir> <dest_dir>" >&2
exit 1
}
[[ $# -lt 2 ]] && usage
```
`>&2` redirects the message to standard error so it does not pollute standard output. The script then exits with code `1`, which signals a usage error to whatever called it.
Every process exits with a number from 0 to 255. The universal convention:
| Code | Meaning |
|------|---------|
| 0 | Success |
| 1 | General or usage error |
| 2 | Misuse of shell built-in |
| Any non-zero | Something failed |
For scripts you write, define specific codes for each failure type and document them in a comment block at the top. For example:
```bash
```
The calling process (another script, a CI/CD pipeline, or cron) can read `$?` immediately after your script runs to decide what to do next.
A log file gives you a record of what happened and when. A minimal logging pattern:
```bash
LOGFILE="/var/log/deploy_check.log"
log() {
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $*" >> "$LOGFILE"
}
log "INFO Script started"
if [[ ! -d "$SOURCE" ]]; then
log "ERROR Source directory $SOURCE not found"
exit 2
fi
log "INFO All checks passed"
```
Note on permissions: `/var/log/` is owned by root on most Linux systems. When running scripts as a non-root user, either use `sudo` carefully, choose a log path your user owns (such as `~/logs/`), or have an administrator pre-create the file with appropriate ownership. Never run a production script as root unless absolutely required — apply the principle of least privilege.
Cron is the standard Linux job scheduler. Each user has a personal crontab file edited with:
```bash
crontab -e
```
A crontab entry has five time fields followed by the command:
```
0 7 1-5 /home/user/deploy_check.sh >> /home/user/logs/deploy_check.log 2>&1
```
Always use absolute paths in crontab entries. Cron runs with a minimal environment and will not find scripts or interpreters through your interactive `PATH`. Verify entries with `crontab -l`.
The best way to verify error handling is to deliberately break things in a controlled way:
1. Remove the file your script expects — confirm a non-zero exit code and a log entry with a timestamp.
2. Bind a port with `nc -l 8080 &` — confirm the port-busy branch fires.
3. Call the script with no arguments — confirm `usage()` prints to stderr and exits with code 1.
Checking `echo $?` immediately after the script exits shows the exit code. Reading the log file with `tail -20 /path/to/logfile` confirms the timestamped entries.
Answer each question, then check it. Nothing is recorded; this is your own practice.
A free tech program — complimentary, as our commitment to get America working. Help us build VoTechWorks. Tell us what works, what doesn’t, and what you want to learn.