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自动化