[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"skill:01M7F1QJGRQ1FDCGFESYR2X0QW":3,"skill-same:01M7F1QJGRQ1FDCGFESYR2X0QW":53},{"skillNo":4,"name":5,"nameZh":6,"description":7,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":16,"categories":17,"keywords":18,"publishedAt":19,"linkOnly":20,"markdownContent":21,"markdownBody":22,"frontMatter":23,"refName":25,"commitSha":26,"skillDirectory":27,"forksCount":14,"repositoryHtmlUrl":28,"repositoryHomepageUrl":6,"ownerHtmlUrl":29,"ownerBlogUrl":30,"ownerDescription":31,"updatedAt":32,"install":33,"resources":46},"01M7F1QJGRQ1FDCGFESYR2X0QW","bash-defensive-patterns",null,"Script skeleton and defensive patterns for production Bash - strict mode, version checks, getopts parsing, temp-file cleanup, error traps and the set -e traps that silently skip errors. Use when writing or hardening a Bash script, CI step or system utility that must fail loudly and clean up after itself.","shotcowboystyle\u002Fag-skills","shotcowboystyle","https:\u002F\u002Favatars.githubusercontent.com\u002Fu\u002F62471?v=4","01M7EZTCFCZJ6K3MYW8BPM4X6R","Curtis Blanton","01M7EZTCG1S5M04J90A931X9BX",0,"MIT","https:\u002F\u002Fgithub.com\u002Fshotcowboystyle\u002Fag-skills\u002Fblob\u002Fmain\u002Fplugins\u002Fshell-scripting\u002Fskills\u002Fbash-defensive-patterns\u002FSKILL.md",[],[],"2026-11-16 10:05:34",false,"---\nname: bash-defensive-patterns\ndescription: Script skeleton and defensive patterns for production Bash - strict mode, version checks, getopts parsing, temp-file cleanup, error traps and the set -e traps that silently skip errors. Use when writing or hardening a Bash script, CI step or system utility that must fail loudly and clean up after itself.\nlast_verified: 2026-10-27\n---\n\n# Bash Defensive Patterns\n\nA script skeleton to start from, and the rules that keep `set -e` from lying to you.\n\n## When to Use This Skill\n\n- Starting a new Bash script or CLI tool\n- Hardening a script that fails silently, leaves temp files behind, or breaks on spaces in\n  file names\n- Reviewing a CI step or deploy script before it runs in production\n\n## Script Skeleton\n\nCopy this, then replace the body of `main`. It runs clean under `shellcheck`, refuses to\nstart on Bash older than 4.4, supports `-n` (dry run) and `-v`, and removes its temp\ndirectory on any exit.\n\n```bash\n#!\u002Fusr\u002Fbin\u002Fenv bash\n# archive-dir: write SRC as a dated .tar.gz into DEST.\n# Requires: bash >= 4.4, tar. Exit codes: 0 ok, 1 error, 2 usage.\nset -Eeuo pipefail\n\nbash_at_least() { (( BASH_VERSINFO[0] > $1 || (BASH_VERSINFO[0] == $1 && BASH_VERSINFO[1] >= $2) )); }\nbash_at_least 4 4 || { printf 'error: needs bash >= 4.4, found %s\\n' \"$BASH_VERSION\" >&2; exit 1; }\nshopt -s inherit_errexit\n\nSCRIPT_NAME=${0##*\u002F}\nreadonly SCRIPT_NAME\ndry_run=0\nverbose=0\ntmpdir=''\n\nusage() {\n  cat \u003C\u003CEOF\nUsage: $SCRIPT_NAME [-n] [-v] SRC DEST\nWrite SRC as a dated .tar.gz archive into DEST.\n\n  -n  dry run: print commands instead of running them\n  -v  verbose\n  -h  show this help\nEOF\n}\n\ndie() { printf '%s: error: %s\\n' \"$SCRIPT_NAME\" \"$*\" >&2; exit 1; }\n\nlog() {\n  if (( verbose )); then printf '%s: %s\\n' \"$SCRIPT_NAME\" \"$*\" >&2; fi\n}\n\nrun() {\n  if (( dry_run )); then\n    printf 'would run:'\n    printf ' %q' \"$@\"\n    printf '\\n'\n  else\n    \"$@\"\n  fi\n}\n\ncleanup() {\n  if [[ -n $tmpdir ]]; then rm -rf -- \"$tmpdir\"; fi\n}\n\non_error() {\n  printf '%s: line %s: exit %s: %s\\n' \"$SCRIPT_NAME\" \"$1\" \"$2\" \"$3\" >&2\n}\n\nmain() {\n  local opt\n  while getopts ':nvh' opt; do\n    case $opt in\n      n) dry_run=1 ;;\n      v) verbose=1 ;;\n      h) usage; exit 0 ;;\n      *) usage >&2; exit 2 ;;\n    esac\n  done\n  shift $((OPTIND - 1))\n  (( $# == 2 )) || { usage >&2; exit 2; }\n\n  local -r src=$1 dest=$2\n  [[ -d $src ]] || die \"not a directory: $src\"\n  [[ -d $dest && -w $dest ]] || die \"not a writable directory: $dest\"\n  command -v tar >\u002Fdev\u002Fnull || die \"tar not found\"\n\n  trap cleanup EXIT\n  trap 'on_error \"$LINENO\" \"$?\" \"$BASH_COMMAND\"' ERR\n  tmpdir=$(mktemp -d)\n\n  local archive\n  archive=\"$dest\u002F$(basename -- \"$src\")-$(date +%Y%m%d).tar.gz\"\n  log \"writing $archive\"\n  run tar -czf \"$tmpdir\u002Fout.tar.gz\" -C \"$src\" .\n  run mv -- \"$tmpdir\u002Fout.tar.gz\" \"$archive\"\n}\n\nmain \"$@\"\n```\n\nWhat each part is for:\n\n- **`bash_at_least`** compares the minor version only when the major is equal. Testing\n  major `>= 4` and minor `>= 4` separately rejects Bash 5.0–5.3. Run it\n  before `shopt -s inherit_errexit`, which older Bash rejects.\n- **`set -Eeuo pipefail`**: `-e` exits on an unchecked failure, `-u` on an unset variable,\n  `-o pipefail` makes a pipeline fail if any stage fails, `-E` lets functions inherit the\n  `ERR` trap. `inherit_errexit` keeps `-e` active inside `$( … )`.\n- **`main \"$@\"` at the bottom**: the whole file is parsed before anything runs, so a\n  half-downloaded script does nothing.\n- **Exit codes**: 0 success, 1 runtime error (`die`), 2 usage error.\n- **`run`** prints the exact command, shell-quoted with `%q`, in dry-run mode.\n\n## Where `set -e` Does Not Stop the Script\n\n`set -e` is ignored in some places and fires unexpectedly in others. Check each of these:\n\n| Pattern | What happens | Fix |\n|---|---|---|\n| `if f; then`, `f && x`, `f \\|\\| x`, `! f` | `set -e` is off for everything `f` runs, including inside `f` | check each command in `f` explicitly, or call `f` on its own line |\n| `local x=$(cmd)`, `export x=$(cmd)` | the exit status is `local`'s, so a failing `cmd` is ignored | `local x; x=$(cmd)` |\n| `(( i++ ))` when `i` is 0 | the expression is 0, status is 1, the script exits | `i=$((i + 1))` |\n| `[[ $v ]] && echo x` as a function's last line | the function returns 1 when `$v` is empty, and the caller exits | use `if … fi` |\n| `cmd \\| head -1` under `pipefail` | `cmd` can die of SIGPIPE (status 141) and fail the pipeline | read the full output, or accept 141 explicitly |\n| `cmd1 $(cmd2)` | the failure of `cmd2` is ignored | assign `cmd2`'s output to a variable first |\n\n## Rules\n\n1. **Quote every expansion**: `\"$var\"`, `\"${arr[@]}\"`, `\"$(cmd)\"`. Unquoted, a file name\n   with a space becomes two arguments and `*` expands to the directory listing.\n2. **Put `--` before user-supplied paths**: `rm -rf -- \"$dir\"`, `mv -- \"$a\" \"$b\"`.\n3. **Guard destructive paths**: `rm -rf -- \"${dir:?}\u002F\"*` stops if `dir` is empty instead\n   of deleting `\u002F*`.\n4. **Iterate files NUL-safely**: `find … -print0 | xargs -0 …` or\n   `while IFS= read -r -d '' f; do …; done \u003C \u003C(find … -print0)`. Never `for f in $(ls)`.\n5. **Build commands as arrays**, never as strings for `eval`:\n   `args=(--verbose); [[ $dry ]] && args+=(--dry-run); tool \"${args[@]}\"`.\n6. **Require environment variables up front**: `: \"${API_URL:?API_URL must be set}\"`.\n7. **Check for commands with `command -v`**, not `which`.\n8. **Use `printf`**, not `echo`, for any data that might start with `-` or contain `\\`.\n9. **Write files atomically**: write to a temp file in the target's directory, then `mv`.\n10. **Lint and test**: `shellcheck -x` (see `shellcheck-configuration`) and Bats tests (see\n    `bats-testing-patterns`).\n\n## More Patterns\n\n`references\u002Fdetails.md` has worked examples for long-option parsing, logging, signal\nhandling for background jobs, atomic writes, idempotent setup, dry-run wrappers and\ndependency checks.\n","# Bash Defensive Patterns\n\nA script skeleton to start from, and the rules that keep `set -e` from lying to you.\n\n## When to Use This Skill\n\n- Starting a new Bash script or CLI tool\n- Hardening a script that fails silently, leaves temp files behind, or breaks on spaces in\n  file names\n- Reviewing a CI step or deploy script before it runs in production\n\n## Script Skeleton\n\nCopy this, then replace the body of `main`. It runs clean under `shellcheck`, refuses to\nstart on Bash older than 4.4, supports `-n` (dry run) and `-v`, and removes its temp\ndirectory on any exit.\n\n```bash\n#!\u002Fusr\u002Fbin\u002Fenv bash\n# archive-dir: write SRC as a dated .tar.gz into DEST.\n# Requires: bash >= 4.4, tar. Exit codes: 0 ok, 1 error, 2 usage.\nset -Eeuo pipefail\n\nbash_at_least() { (( BASH_VERSINFO[0] > $1 || (BASH_VERSINFO[0] == $1 && BASH_VERSINFO[1] >= $2) )); }\nbash_at_least 4 4 || { printf 'error: needs bash >= 4.4, found %s\\n' \"$BASH_VERSION\" >&2; exit 1; }\nshopt -s inherit_errexit\n\nSCRIPT_NAME=${0##*\u002F}\nreadonly SCRIPT_NAME\ndry_run=0\nverbose=0\ntmpdir=''\n\nusage() {\n  cat \u003C\u003CEOF\nUsage: $SCRIPT_NAME [-n] [-v] SRC DEST\nWrite SRC as a dated .tar.gz archive into DEST.\n\n  -n  dry run: print commands instead of running them\n  -v  verbose\n  -h  show this help\nEOF\n}\n\ndie() { printf '%s: error: %s\\n' \"$SCRIPT_NAME\" \"$*\" >&2; exit 1; }\n\nlog() {\n  if (( verbose )); then printf '%s: %s\\n' \"$SCRIPT_NAME\" \"$*\" >&2; fi\n}\n\nrun() {\n  if (( dry_run )); then\n    printf 'would run:'\n    printf ' %q' \"$@\"\n    printf '\\n'\n  else\n    \"$@\"\n  fi\n}\n\ncleanup() {\n  if [[ -n $tmpdir ]]; then rm -rf -- \"$tmpdir\"; fi\n}\n\non_error() {\n  printf '%s: line %s: exit %s: %s\\n' \"$SCRIPT_NAME\" \"$1\" \"$2\" \"$3\" >&2\n}\n\nmain() {\n  local opt\n  while getopts ':nvh' opt; do\n    case $opt in\n      n) dry_run=1 ;;\n      v) verbose=1 ;;\n      h) usage; exit 0 ;;\n      *) usage >&2; exit 2 ;;\n    esac\n  done\n  shift $((OPTIND - 1))\n  (( $# == 2 )) || { usage >&2; exit 2; }\n\n  local -r src=$1 dest=$2\n  [[ -d $src ]] || die \"not a directory: $src\"\n  [[ -d $dest && -w $dest ]] || die \"not a writable directory: $dest\"\n  command -v tar >\u002Fdev\u002Fnull || die \"tar not found\"\n\n  trap cleanup EXIT\n  trap 'on_error \"$LINENO\" \"$?\" \"$BASH_COMMAND\"' ERR\n  tmpdir=$(mktemp -d)\n\n  local archive\n  archive=\"$dest\u002F$(basename -- \"$src\")-$(date +%Y%m%d).tar.gz\"\n  log \"writing $archive\"\n  run tar -czf \"$tmpdir\u002Fout.tar.gz\" -C \"$src\" .\n  run mv -- \"$tmpdir\u002Fout.tar.gz\" \"$archive\"\n}\n\nmain \"$@\"\n```\n\nWhat each part is for:\n\n- **`bash_at_least`** compares the minor version only when the major is equal. Testing\n  major `>= 4` and minor `>= 4` separately rejects Bash 5.0–5.3. Run it\n  before `shopt -s inherit_errexit`, which older Bash rejects.\n- **`set -Eeuo pipefail`**: `-e` exits on an unchecked failure, `-u` on an unset variable,\n  `-o pipefail` makes a pipeline fail if any stage fails, `-E` lets functions inherit the\n  `ERR` trap. `inherit_errexit` keeps `-e` active inside `$( … )`.\n- **`main \"$@\"` at the bottom**: the whole file is parsed before anything runs, so a\n  half-downloaded script does nothing.\n- **Exit codes**: 0 success, 1 runtime error (`die`), 2 usage error.\n- **`run`** prints the exact command, shell-quoted with `%q`, in dry-run mode.\n\n## Where `set -e` Does Not Stop the Script\n\n`set -e` is ignored in some places and fires unexpectedly in others. Check each of these:\n\n| Pattern | What happens | Fix |\n|---|---|---|\n| `if f; then`, `f && x`, `f \\|\\| x`, `! f` | `set -e` is off for everything `f` runs, including inside `f` | check each command in `f` explicitly, or call `f` on its own line |\n| `local x=$(cmd)`, `export x=$(cmd)` | the exit status is `local`'s, so a failing `cmd` is ignored | `local x; x=$(cmd)` |\n| `(( i++ ))` when `i` is 0 | the expression is 0, status is 1, the script exits | `i=$((i + 1))` |\n| `[[ $v ]] && echo x` as a function's last line | the function returns 1 when `$v` is empty, and the caller exits | use `if … fi` |\n| `cmd \\| head -1` under `pipefail` | `cmd` can die of SIGPIPE (status 141) and fail the pipeline | read the full output, or accept 141 explicitly |\n| `cmd1 $(cmd2)` | the failure of `cmd2` is ignored | assign `cmd2`'s output to a variable first |\n\n## Rules\n\n1. **Quote every expansion**: `\"$var\"`, `\"${arr[@]}\"`, `\"$(cmd)\"`. Unquoted, a file name\n   with a space becomes two arguments and `*` expands to the directory listing.\n2. **Put `--` before user-supplied paths**: `rm -rf -- \"$dir\"`, `mv -- \"$a\" \"$b\"`.\n3. **Guard destructive paths**: `rm -rf -- \"${dir:?}\u002F\"*` stops if `dir` is empty instead\n   of deleting `\u002F*`.\n4. **Iterate files NUL-safely**: `find … -print0 | xargs -0 …` or\n   `while IFS= read -r -d '' f; do …; done \u003C \u003C(find … -print0)`. Never `for f in $(ls)`.\n5. **Build commands as arrays**, never as strings for `eval`:\n   `args=(--verbose); [[ $dry ]] && args+=(--dry-run); tool \"${args[@]}\"`.\n6. **Require environment variables up front**: `: \"${API_URL:?API_URL must be set}\"`.\n7. **Check for commands with `command -v`**, not `which`.\n8. **Use `printf`**, not `echo`, for any data that might start with `-` or contain `\\`.\n9. **Write files atomically**: write to a temp file in the target's directory, then `mv`.\n10. **Lint and test**: `shellcheck -x` (see `shellcheck-configuration`) and Bats tests (see\n    `bats-testing-patterns`).\n\n## More Patterns\n\n`references\u002Fdetails.md` has worked examples for long-option parsing, logging, signal\nhandling for background jobs, atomic writes, idempotent setup, dry-run wrappers and\ndependency checks.\n",{"name":5,"description":7,"last_verified":24},1793059200000,"main","f846d9a97082ff30d613b55a085cef425d822473","plugins\u002Fshell-scripting\u002Fskills\u002Fbash-defensive-patterns","https:\u002F\u002Fgithub.com\u002Fshotcowboystyle\u002Fag-skills","https:\u002F\u002Fgithub.com\u002Fshotcowboystyle","https:\u002F\u002Fshotcowboystyle.github.io","Building smarter with AI, automation & modern tools. Exploring how far one person can go without a big team using today’s AI.","2026-11-10 04:58:27",{"source":8,"skillName":5,"commands":34,"prompt":44,"zipAvailable":45},[35,38,41],{"tool":36,"command":37},"npx","npx skills add shotcowboystyle\u002Fag-skills --skill bash-defensive-patterns",{"tool":39,"command":40},"bunx","bunx skills add shotcowboystyle\u002Fag-skills --skill bash-defensive-patterns",{"tool":42,"command":43},"pnpm","pnpm dlx skills add shotcowboystyle\u002Fag-skills --skill bash-defensive-patterns","安装技能，技能地址：https:\u002F\u002Fgithub.com\u002Fshotcowboystyle\u002Fag-skills\u002Fblob\u002Fmain\u002Fplugins\u002Fshell-scripting\u002Fskills\u002Fbash-defensive-patterns\u002FSKILL.md",true,[47],{"path":48,"resourceType":49,"resourceTypeDesc":50,"sizeBytes":51,"sourceHtmlUrl":52},"references\u002Fdetails.md","REFERENCE","参考文档",9945,"https:\u002F\u002Fgithub.com\u002Fshotcowboystyle\u002Fag-skills\u002Fblob\u002Fmain\u002Fplugins\u002Fshell-scripting\u002Fskills\u002Fbash-defensive-patterns\u002Freferences\u002Fdetails.md",[54,60,67,74,81,88],{"skillNo":55,"name":56,"nameZh":6,"description":6,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":57,"categories":58,"keywords":59,"publishedAt":19},"01M7F1YRY5S5QRJ1V0BJS130QX","no-description","https:\u002F\u002Fgithub.com\u002Fshotcowboystyle\u002Fag-skills\u002Fblob\u002Fmain\u002Ftests\u002Ffixtures\u002Fno-description\u002FSKILL.md",[],[],{"skillNo":61,"name":62,"nameZh":6,"description":63,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":64,"categories":65,"keywords":66,"publishedAt":19},"01M7F1Y9BV2FJNQJ3NB32JS5CM","good-skill","Fixture skill that satisfies every rule.","https:\u002F\u002Fgithub.com\u002Fshotcowboystyle\u002Fag-skills\u002Fblob\u002Fmain\u002Ftests\u002Ffixtures\u002Fgood-skill\u002FSKILL.md",[],[],{"skillNo":68,"name":69,"nameZh":6,"description":70,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":71,"categories":72,"keywords":73,"publishedAt":19},"01M7F1XXBQ3NQRXZSS0KAGAR9V","broken-link","Links to a reference file that does not exist.","https:\u002F\u002Fgithub.com\u002Fshotcowboystyle\u002Fag-skills\u002Fblob\u002Fmain\u002Ftests\u002Ffixtures\u002Fbroken-link\u002FSKILL.md",[],[],{"skillNo":75,"name":76,"nameZh":6,"description":77,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":78,"categories":79,"keywords":80,"publishedAt":19},"01M7F1XBX4R46NR9W0HXQ7XCDX","Bad_Name","Name violates the kebab-case rule and does not match the directory.","https:\u002F\u002Fgithub.com\u002Fshotcowboystyle\u002Fag-skills\u002Fblob\u002Fmain\u002Ftests\u002Ffixtures\u002Fbad-name\u002FSKILL.md",[],[],{"skillNo":82,"name":83,"nameZh":6,"description":84,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":85,"categories":86,"keywords":87,"publishedAt":19},"01M7F1WNAF8WNEQ7S5HKT5VK0D","skill-authoring","Write, review, and validate portable agent skills in SKILL.md format. Use when creating a new skill, editing an existing one, or asked whether a skill follows the Agent Skills spec.","https:\u002F\u002Fgithub.com\u002Fshotcowboystyle\u002Fag-skills\u002Fblob\u002Fmain\u002Fskills\u002Fskill-authoring\u002FSKILL.md",[],[],{"skillNo":89,"name":90,"nameZh":6,"description":91,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":92,"categories":93,"keywords":94,"publishedAt":19},"01M7F1W30TC0XFNSF7YBRGETK2","weigh-bags","Record scale readings for each bag — empty (tare), outbound, current, final — and reconcile them against the itemised inventory to find unaccounted weight. Use when the user gives a bag weight, says how heavy something is, weighs their luggage, or asks how close to the limit they are. Appends to bags.yaml and runs scripts\u002Freconcile.py.","https:\u002F\u002Fgithub.com\u002Fshotcowboystyle\u002Fag-skills\u002Fblob\u002Fmain\u002Fplugins\u002Ftravel-packing-assistant\u002Fskills\u002Fweigh-bags\u002FSKILL.md",[],[]]