bash-scripting

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Bash Scripting Workflow

Bash脚本编写工作流

Overview

概述

Specialized workflow for creating robust, production-ready bash scripts with defensive programming patterns, comprehensive error handling, and automated testing.
专为创建稳健、生产级Bash脚本设计的专业化工作流,涵盖防御性编程模式、全面的错误处理以及自动化测试。

When to Use This Workflow

适用场景

Use this workflow when:
  • Creating automation scripts
  • Writing system administration tools
  • Building deployment scripts
  • Developing backup solutions
  • Creating CI/CD scripts
在以下场景中使用此工作流:
  • 创建自动化脚本
  • 编写系统管理工具
  • 构建部署脚本
  • 开发备份解决方案
  • 创建CI/CD脚本

Workflow Phases

工作流阶段

Phase 1: Script Design

阶段1:脚本设计

Skills to Invoke

调用技能

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

Actions

操作步骤

  1. Define script purpose
  2. Identify inputs/outputs
  3. Plan error handling
  4. Design logging strategy
  5. Document requirements
  1. 定义脚本用途
  2. 确定输入/输出
  3. 规划错误处理方案
  4. 设计日志策略
  5. 记录需求文档

Copy-Paste Prompts

可复制提示语

Use @bash-pro to design production-ready bash script
Use @bash-pro to design production-ready bash script

Phase 2: Script Structure

阶段2:脚本结构

Skills to Invoke

调用技能

  • bash-pro
    - Script structure
  • bash-defensive-patterns
    - Safety patterns
  • bash-pro
    - 脚本结构设计
  • bash-defensive-patterns
    - 安全模式

Actions

操作步骤

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

Copy-Paste Prompts

可复制提示语

Use @bash-defensive-patterns to implement strict mode and error handling
Use @bash-defensive-patterns to implement strict mode and error handling

Phase 3: Core Implementation

阶段3:核心实现

Skills to Invoke

调用技能

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

Actions

操作步骤

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

Copy-Paste Prompts

可复制提示语

Use @bash-linux to implement system commands
Use @bash-linux to implement system commands

Phase 4: Error Handling

阶段4:错误处理

Skills to Invoke

调用技能

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

Actions

操作步骤

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

Copy-Paste Prompts

可复制提示语

Use @bash-defensive-patterns to add comprehensive error handling
Use @bash-defensive-patterns to add comprehensive error handling

Phase 5: Logging

阶段5:日志功能

Skills to Invoke

调用技能

  • bash-pro
    - Logging patterns
  • bash-pro
    - 日志模式

Actions

操作步骤

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

Copy-Paste Prompts

可复制提示语

Use @bash-pro to implement structured logging
Use @bash-pro to implement structured logging

Phase 6: Testing

阶段6:测试

Skills to Invoke

调用技能

  • bats-testing-patterns
    - Bats testing
  • shellcheck-configuration
    - ShellCheck
  • bats-testing-patterns
    - Bats测试
  • shellcheck-configuration
    - ShellCheck

Actions

操作步骤

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

Copy-Paste Prompts

可复制提示语

Use @bats-testing-patterns to write script tests
Use @shellcheck-configuration to lint bash script
Use @bats-testing-patterns to write script tests
Use @shellcheck-configuration to lint bash script

Phase 7: Documentation

阶段7:文档编写

Skills to Invoke

调用技能

  • documentation-templates
    - Documentation
  • documentation-templates
    - 文档模板

Actions

操作步骤

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

Copy-Paste Prompts

可复制提示语

Use @documentation-templates to document bash script
Use @documentation-templates to document bash script

Script Template

脚本模板

bash
#!/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
Usage: $SCRIPT_NAME [OPTIONS]
Options:
    -h, --help      Show help
    -v, --verbose   Verbose output
EOF
}

main() {
    log "Script started"
    # Implementation
    log "Script completed"
}

main "$@"
bash
#!/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
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 passes
  • Bats tests pass
  • Error handling works
  • Logging functional
  • Documentation complete
  • 通过ShellCheck检查
  • 通过Bats测试
  • 错误处理功能正常
  • 日志功能可用
  • 文档完整

Related Workflow Bundles

相关工作流包

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

Limitations

限制说明

  • Use this skill only when the task clearly matches the scope described above.
  • Do not treat the output as a substitute for environment-specific validation, testing, or expert review.
  • Stop and ask for clarification if required inputs, permissions, safety boundaries, or success criteria are missing.
  • 仅当任务明确符合上述描述的范围时使用此技能。
  • 不要将输出结果视为环境特定验证、测试或专家评审的替代品。
  • 如果缺少必要的输入、权限、安全边界或成功标准,请暂停并请求澄清。