AgentSkillsCN

shell-scripting

创建、重构或审查 Bash Shell 脚本,严格遵循脚本编写规范,采用安全的参数解析、幂等性行为,并输出丰富的 CLI 输出内容(结构化日志,搭配易读的使用/帮助信息,同时支持彩色回退)。当用户寻求 .sh 脚本、Shell 自动化、CLI 包装器,或脚本加固方案时,可使用此技能。

SKILL.md
--- frontmatter
name: shell-scripting
description: Create, refactor, or review Bash shell scripts with strict mode, safe argument parsing, idempotent behavior, and rich CLI output (structured logs plus readable usage/help with color fallback). Use when users ask for .sh scripts, shell automation, CLI wrappers, or script hardening.

Shell Scripting

Overview

Write scripts that are safe by default, easy to read, and predictable across repeated runs. For new scripts, start from assets/script-template.sh.

Workflow

  1. Confirm scope and runtime assumptions (bash version, OS expectations, required tools).
  2. Start from assets/script-template.sh and adapt it.
  3. Keep parsing explicit (-h, --help, --) and fail on unknown flags.
  4. Keep operations idempotent where practical (safe re-runs, no destructive defaults).
  5. Validate with:
    • bash -n <script>
    • a small functional smoke run with safe inputs

Output Style

Use structured output helpers:

  • log_error: red + bold prefix (ERROR:), stderr
  • log_warn: yellow, stderr
  • log_note: dim informational note
  • log_success: green success output

Use ANSI styling only when interactive:

  • enable color when -t 1 or -t 2
  • disable color when TERM=dumb or NO_COLOR is set

Keep usage output readable:

  • bold section headers (Usage, Options, Examples)
  • highlight command examples in cyan
  • keep notes concise and actionable

Core Practices

  • Use #!/usr/bin/env bash and set -euo pipefail.
  • Quote variable expansions unless intentionally using word splitting.
  • Use local for function-scoped variables.
  • Prefer printf over echo for structured output.
  • Validate required commands early (for example with command -v checks).
  • Use mktemp for temporary files/dirs and cleanup with trap.
  • Avoid destructive actions by default; require explicit opt-in for risky actions.
  • Keep scripts composable and testable with small functions.

Resources

  • assets/script-template.sh: baseline script template with rich usage/log output.
  • references/best-practices.md: expanded checklist and patterns.