🚀 快速安装

复制以下命令并运行,立即安装此 Skill:

npx skills add https://skills.sh/sickn33/antigravity-awesome-skills/bash-scripting

💡 提示:需要 Node.js 和 NPM

Bash 脚本编写工作流 (Bash Scripting Workflow)

概述 (Overview)

专门用于创建健壮、生产就绪的 Bash 脚本的工作流,采用防御性编程模式、全面的错误处理和自动化测试。

何时使用此工作流 (When to Use This Workflow)

在以下情况下使用此工作流:

  • 创建自动化脚本
  • 编写系统管理工具
  • 构建部署脚本
  • 开发备份解决方案
  • 创建 CI/CD 脚本

工作流阶段 (Workflow Phases)

阶段 1:脚本设计 (Phase 1: Script Design)

调用的技能 (Skills to Invoke)

  • bash-pro – 专业脚本编写 (Professional scripting)
  • bash-defensive-patterns – 防御性模式 (Defensive patterns)

操作 (Actions)

  1. 定义脚本目的 (Define script purpose)
  2. 识别输入/输出 (Identify inputs/outputs)
  3. 规划错误处理 (Plan error handling)
  4. 设计日志策略 (Design logging strategy)
  5. 记录需求 (Document requirements)

复制粘贴提示 (Copy-Paste Prompts)

使用 @bash-pro 设计生产就绪的 bash 脚本 (Use @bash-pro to design production-ready bash script)

阶段 2:脚本结构 (Phase 2: Script Structure)

调用的技能 (Skills to Invoke)

  • bash-pro – 脚本结构 (Script structure)
  • bash-defensive-patterns – 安全模式 (Safety patterns)

操作 (Actions)

  1. 添加 shebang 和严格模式 (Add shebang and strict mode)
  2. 创建用法函数 (Create usage function)
  3. 实现参数解析 (Implement argument parsing)
  4. 设置日志记录 (Set up logging)
  5. 添加清理处理程序 (Add cleanup handlers)

复制粘贴提示 (Copy-Paste Prompts)

使用 @bash-defensive-patterns 实现严格模式和错误处理 (Use @bash-defensive-patterns to implement strict mode and error handling)

阶段 3:核心实现 (Phase 3: Core Implementation)

调用的技能 (Skills to Invoke)

  • bash-linux – Linux 命令 (Linux commands)
  • linux-shell-scripting – Shell 脚本编写 (Shell scripting)

操作 (Actions)

  1. 实现主要函数 (Implement main functions)
  2. 添加输入验证 (Add input validation)
  3. 创建辅助函数 (Create helper functions)
  4. 处理边缘情况 (Handle edge cases)
  5. 添加进度指示器 (Add progress indicators)

复制粘贴提示 (Copy-Paste Prompts)

使用 @bash-linux 实现系统命令 (Use @bash-linux to implement system commands)

阶段 4:错误处理 (Phase 4: Error Handling)

调用的技能 (Skills to Invoke)

  • bash-defensive-patterns – 错误处理 (Error handling)
  • error-handling-patterns – 错误模式 (Error patterns)

操作 (Actions)

  1. 添加 trap 处理程序 (Add trap handlers)
  2. 实现重试逻辑 (Implement retry logic)
  3. 创建错误消息 (Create error messages)
  4. 设置退出代码 (Set up exit codes)
  5. 添加回滚能力 (Add rollback capability)

复制粘贴提示 (Copy-Paste Prompts)

使用 @bash-defensive-patterns 添加全面的错误处理 (Use @bash-defensive-patterns to add comprehensive error handling)

阶段 5:日志记录 (Phase 5: Logging)

调用的技能 (Skills to Invoke)

  • bash-pro – 日志模式 (Logging patterns)

操作 (Actions)

  1. 创建日志函数 (Create logging function)
  2. 添加日志级别 (Add log levels)
  3. 实现时间戳 (Implement timestamps)
  4. 配置日志轮转 (Configure log rotation)
  5. 添加调试模式 (Add debug mode)

复制粘贴提示 (Copy-Paste Prompts)

使用 @bash-pro 实现结构化日志 (Use @bash-pro to implement structured logging)

阶段 6:测试 (Phase 6: Testing)

调用的技能 (Skills to Invoke)

  • bats-testing-patterns – Bats 测试 (Bats testing)
  • shellcheck-configuration – ShellCheck

操作 (Actions)

  1. 编写 Bats 测试 (Write Bats tests)
  2. 运行 ShellCheck (Run ShellCheck)
  3. 测试边缘情况 (Test edge cases)
  4. 验证错误处理 (Verify error handling)
  5. 使用不同输入进行测试 (Test with different inputs)

复制粘贴提示 (Copy-Paste Prompts)

使用 @bats-testing-patterns 编写脚本测试 (Use @bats-testing-patterns to write script tests)
使用 @shellcheck-configuration 对 bash 脚本进行 lint (Use @shellcheck-configuration to lint bash script)

阶段 7:文档 (Phase 7: Documentation)

调用的技能 (Skills to Invoke)

  • documentation-templates – 文档 (Documentation)

操作 (Actions)

  1. 添加脚本头 (Add script header)
  2. 记录函数 (Document functions)
  3. 创建使用示例 (Create usage examples)
  4. 列出依赖项 (List dependencies)
  5. 添加故障排除部分 (Add troubleshooting section)

复制粘贴提示 (Copy-Paste Prompts)

使用 @documentation-templates 记录 bash 脚本 (Use @documentation-templates to document bash script)

脚本模板 (Script Template)

#!/usr/bin/env bash
set -euo pipefail

readonly SCRIPT_NAME=$(basename "$0")
readonly SCRIPT_DIR=$(cd "$(dirname "$0")" && pwd)

log() { echo "[$(date '+%Y-%m-%d %H:%M:%S')] $*"; }
error() { log "ERROR: $*" >&2; exit 1; }

usage() { cat <<EOF
用法: $SCRIPT_NAME [选项] (Usage: $SCRIPT_NAME [OPTIONS])
选项 (Options):
    -h, --help      显示帮助 (Show help)
    -v, --verbose   详细输出 (Verbose output)
EOF
}

main() {
    log "脚本已启动 (Script started)"
    # 实现 (Implementation)
    log "脚本已完成 (Script completed)"
}

main "$@"

质量关卡 (Quality Gates)

  • ShellCheck 通过 (ShellCheck passes)
  • Bats 测试通过 (Bats tests pass)
  • 错误处理有效 (Error handling works)
  • 日志功能正常 (Logging functional)
  • 文档完整 (Documentation complete)

相关工作流捆绑包 (Related Workflow Bundles)

  • os-scripting – 操作系统脚本编写 (OS scripting)
  • linux-troubleshooting – Linux 故障排除 (Linux troubleshooting)
  • cloud-devops – DevOps 自动化 (DevOps automation)

📄 原始文档

完整文档(英文):

https://skills.sh/sickn33/antigravity-awesome-skills/bash-scripting

💡 提示:点击上方链接查看 skills.sh 原始英文文档,方便对照翻译。

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。