← ClaudeAtlas

bash-scriptinglisted

When to activate: bash, shell script, sh, set -e, trap, argument parsing, heredoc, parallel, cron script, automation
Mattakushi432/Claude-Code-Skills-Custom-DevTools-Pack · ★ 0 · AI & Automation · score 73
Install: claude install-skill Mattakushi432/Claude-Code-Skills-Custom-DevTools-Pack
# Bash Scripting Patterns ## Script Header (always use) ```bash #!/usr/bin/env bash set -euo pipefail IFS=$'\n\t' readonly SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" readonly SCRIPT_NAME="$(basename "$0")" ``` ## Error Handling with trap ```bash cleanup() { local exit_code=$? echo "[${SCRIPT_NAME}] Cleaning up (exit: ${exit_code})" >&2 rm -f /tmp/myapp-lock exit "${exit_code}" } trap cleanup EXIT INT TERM die() { echo "[ERROR] $*" >&2 exit 1 } [[ -f config.yaml ]] || die "config.yaml not found" ``` ## Argument Parsing ```bash usage() { cat <<EOF Usage: ${SCRIPT_NAME} [OPTIONS] Options: -e, --env ENV Environment (dev|staging|prod) [required] -t, --tag TAG Docker image tag [default: latest] -d, --dry-run Print commands, don't execute -h, --help Show this help EOF } ENV="" TAG="latest" DRY_RUN=false while [[ $# -gt 0 ]]; do case $1 in -e|--env) ENV="$2"; shift 2 ;; -t|--tag) TAG="$2"; shift 2 ;; -d|--dry-run) DRY_RUN=true; shift ;; -h|--help) usage; exit 0 ;; *) die "Unknown option: $1" ;; esac done [[ -n "${ENV}" ]] || die "--env is required" [[ "${ENV}" =~ ^(dev|staging|prod)$ ]] || die "Invalid env: ${ENV}" ``` ## Run or Dry-Run Helper ```bash run() { echo "+ $*" >&2 if [[ "${DRY_RUN}" == "false" ]]; then "$@" fi } run kubectl apply -f deployment.yaml run helm upgrade myapp ./chart --atomic ``` ## Retry with Backoff ```bash r