Skip to content

Latest commit

 

History

History
67 lines (44 loc) · 2.25 KB

File metadata and controls

67 lines (44 loc) · 2.25 KB

Reliable Shell Work

← Guide index · Repository home

Overview

The shell is both an interactive diagnostic environment and a programming language. Safe work depends on quoting, exit status, pipelines, redirection, and explicit failure handling.

Caution

Inspect targets and understand impact before running privileged or mutating commands. Test changes in a safe environment first.

Operational Scenario

A maintenance command must find old report files, preview the candidates, archive them, and leave a useful audit trail without deleting the wrong path.

Core Concepts

# Working principle
1 Every command returns an exit status; zero conventionally means success.
2 Quoting controls expansion and protects whitespace or wildcard characters.
3 A pipeline connects standard output to standard input while standard error remains separate unless redirected.

Investigation Commands

set -o errexit -o nounset -o pipefail

report_root="/srv/reports"
find "$report_root" -type f -name '*.csv' -mtime +30 -print

# Inspect the list before replacing -print with a mutating action.
printf 'exit=%s\n' "$?"

Commands are examples for observation and controlled testing. Paths, process identifiers, interfaces, and service names must be adapted to the actual host.

Practical Workflow

  1. Define the symptom, affected users, and time window.
  2. Collect the smallest useful set of host and service evidence.
  3. Form one falsifiable hypothesis.
  4. Make the least disruptive test or change.
  5. Verify recovery and record what was learned.

Production Practices

  • Use shellcheck for scripts and quote variable expansions by default.
  • Print or log targets before bulk file operations.
  • Prefer small scripts in version control over undocumented command history.

Common Mistakes

  • Parsing ls output.
  • Using rm with an unverified variable or glob.
  • Assuming a pipeline failed only when its final command failed.

Interview and Review Questions

  1. What does pipefail change?
  2. Why does quoting "$variable" matter?
  3. How would you make a maintenance script idempotent?

References