ams-shell-pro
暂无简介
- License 许可
- MIT
- 最近更新
- 2026-11-07
- Stars
- ★ 0
- Forks
- 1
03 SKILL.md 正文
Shell Pro
Overview
Shell scripts are the glue of operations — and the source of legendary 3am failures when
written casually. Professional shell scripting means defensive defaults (set -euo pipefail),
obsessive quoting, explicit error handling, and knowing when the script has outgrown shell
(rewrite in Python/Go at ~200 lines or when you need real data structures).
The through-line: assume every command can fail, every variable can be empty, and every filename can contain spaces.
When to use
- Writing or reviewing Bash/POSIX shell scripts.
- Building CLI wrappers, deploy scripts, or CI helpers.
- Debugging scripts that "work on my machine" but fail in CI.
- Deciding between shell and a "real" language.
- Hardening existing scripts (quoting, error handling, idempotency).
Core concepts
- Strict mode first.
set -euo pipefail— exit on error, error on undefined variables, and fail pipelines when any stage fails. AddIFS=$'\n\t'deliberately when word-splitting behavior matters. This one line prevents entire bug classes. - Quote everything.
"$var","$(cmd)","$@"— always. Unquoted expansions split on whitespace and glob — the source of "works until a filename has a space." The only unquoted things should be intentionally-split lists. - Errors are explicit. Check exit codes where they matter;
cmd || die "context"patterns;trapfor cleanup (temp files, locks) on exit. A script that fails silently is worse than one that crashes loudly. - Functions for structure. Small named functions with
localvariables;main "$@"at the bottom; usage/help text. A 300-line flat script is unmaintainable — structure it like code, because it is. - Idempotency and safety. Scripts that run twice shouldn't break things; destructive operations
get
--dry-runmodes and confirmation prompts;rm -rf "$dir/"with an empty$diris how legends are born — guard it (: "${dir:?dir must be set}"). - Know when to stop. Complex JSON parsing (use
jq, or switch languages), data structures beyond arrays/associative arrays, heavy logic — these are signs to rewrite in Python. Shell is for orchestration, not computation.
Practical workflow
- Start every script with the template:
#!/usr/bin/env bash set -euo pipefail die() { echo "ERROR: $*" >&2; exit 1; } usage() { echo "Usage: $(basename "$0") [options] <arg>"; } - Parse arguments properly.
getoptsfor short flags; explicit--longhandling or a tiny parser for long options. Validate required args and fail with usage on misuse. - Write functions, keep main thin. Each function does one thing;
localall function variables; return codes meaningful (0 = ok). - Handle temp files and cleanup.
tmpdir=$(mktemp -d)+trap 'rm -rf "$tmpdir"' EXIT— always. Lock files for scripts that must not run concurrently (flock). - Test the failure modes. Run with
bash -xto trace; test with empty vars, missing files, spaces in names, and non-zero exits from dependencies. ShellCheck in CI — non-negotiable. - Document the contract. Header comment: purpose, usage, required tools/env vars, exit codes. The next person running this at 3am thanks you.
Robust script skeleton:
#!/usr/bin/env bash
set -euo pipefail
die() { echo "ERROR: $*" >&2; exit 1; }
log() { echo "[$(date -u +%FT%TZ)] $*"; }
main() {
local src="${1:?Usage: $0 <src-dir>}"
[[ -d "$src" ]] || die "not a directory: $src"
local tmp; tmp=$(mktemp -d)
trap 'rm -rf "$tmp"' EXIT
log "processing $src ..."
# ... work, quoting everything ...
log "done"
}
main "$@"
Common pitfalls
- Unquoted variables. The #1 shell bug.
rm -rf $dirwith empty$dirtries to delete everything. Quote. Everything. - No
set -euo pipefail. Scripts that barrel past failures, then report success. Or the subtler: pipelines masking failures (false | truesucceeds without pipefail). set -esurprises. It doesn't trigger in all contexts (conditions,&&/||chains). Know its semantics or check critical commands explicitly.- Parsing
lsoutput. Filenames with newlines/spaces break it. Use globs orfind -print0 | while IFS= read -r -d ''. - curl/wget without failure flags.
curl -o file urlsucceeding with an error page saved as "success." Usecurl -fsSL(fail, silent, show errors, follow redirects). - Assuming GNU tools. BSD vs GNU
sed/date/grepdifferences break scripts across macOS and Linux. Test on both or document the requirement. - Scripts that grow forever. The 800-line Bash deployment script nobody dares touch. Refactor into functions early; rewrite in a real language when structure demands it.
04 同仓库更多 Skills
web-scrape
Turn web pages into LLM-ready markdown via the Firecrawl API (renders JS, anti-bot handling). Use when browser_open/fetch fails on JS-heavy pages, or when you need clean markdown/structured extraction from a site. Requires FIRECRAWL_API_KEY.
design-taste-frontend
tasteskill v2, the anti-slop frontend skill. Art direction for websites, distilled from a study of 247 curated gallery sites (One Page Love, Awwwards Sites of the Day). Deals researched directions (idea, composition, type, palette, photographs, section blueprints) from a library, enforces a strict no-slop house style with a real-browser inspector, and runs a look-and-fix loop on screenshots. Use this whenever you build, design, redesign, restyle, polish or review a landing page, marketing site, portfolio, product page, one-pager, hero or any public-facing web page, in any stack (HTML/CSS, React, Next.js, Vue, Svelte, Astro, Tailwind) — also when the user says a page looks generic, templated, "AI-made", sloppy, or asks to make it look designed, premium, award-level or less like every other site. Not for dashboards or app screens.
test-driven-development
Use when implementing any feature or bugfix, before writing implementation code
systematic-debugging
Use when encountering any bug, test failure, or unexpected behavior, before proposing fixes
requesting-code-review
Use when completing tasks, implementing major features, or before merging to verify work meets requirements
brainstorming
You MUST use this before any creative work - creating features, building components, adding functionality, modifying behavior, or planning anything new, in software or out of it (a talk, a business, a renovation).