Skill 寫法圖鑑

bash-defensive-patterns

wshobson | 架構與重構 | 原文 | MIT

寫可上線等級 Bash script 的防禦式技巧:strict mode、錯誤攔截、安全處理暫存檔

中文翻譯

---
name: bash-defensive-patterns
description: 掌握可上線等級 script 的防禦式 Bash 程式設計技巧。在撰寫穩健的 shell script、CI/CD pipeline,或需要容錯與安全的系統工具時使用。
---

# Bash 防禦式模式

用防禦式程式設計技巧、錯誤處理和安全最佳實務,撰寫可上線的 Bash script 的完整指引,用來避開常見陷阱並確保可靠性。

## 何時使用這個 Skill

- 撰寫正式環境的自動化 script
- 建構 CI/CD pipeline 的 script
- 建立系統管理的工具
- 開發有錯誤韌性的部署自動化
- 撰寫必須安全處理邊界情況的 script
- 建構好維護的 shell script 函式庫
- 實作完整的 logging 和監控
- 建立必須跨不同平台運作的 script

## 詳細模式與實作範例

詳細的模式文件放在 `references/details.md`。上面的導覽層不夠用時,再讀那個檔案。

## 最佳實務摘要

1. **一律使用 strict mode** - `set -Eeuo pipefail`
2. **所有變數都要加引號** - `"$variable"` 可以防止 word splitting
3. **用 [[]] 做條件判斷** - 比 [ ] 更穩健
4. **實作錯誤攔截** - 優雅地抓到並處理錯誤
5. **驗證所有輸入** - 檢查檔案是否存在、權限、格式
6. **用函式做重複使用** - 用有意義的名稱當前綴
7. **實作結構化的 logging** - 包含時間戳記和等級
8. **支援 dry-run 模式** - 讓使用者預覽變更
9. **安全地處理暫存檔** - 用 mktemp,並用 trap 清理
10. **設計成 idempotent** - script 重跑應該是安全的
11. **記錄需求** - 列出相依套件和最低版本
12. **測試錯誤路徑** - 確保錯誤處理確實能運作
13. **用 `command -v`** - 檢查執行檔時比 `which` 更安全
14. **優先用 printf,不要用 echo** - 跨系統的行為更可預測

英文原文

---
name: bash-defensive-patterns
description: Master defensive Bash programming techniques for production-grade scripts. Use when writing robust shell scripts, CI/CD pipelines, or system utilities requiring fault tolerance and safety.
---

# Bash Defensive Patterns

Comprehensive guidance for writing production-ready Bash scripts using defensive programming techniques, error handling, and safety best practices to prevent common pitfalls and ensure reliability.

## When to Use This Skill

- Writing production automation scripts
- Building CI/CD pipeline scripts
- Creating system administration utilities
- Developing error-resilient deployment automation
- Writing scripts that must handle edge cases safely
- Building maintainable shell script libraries
- Implementing comprehensive logging and monitoring
- Creating scripts that must work across different platforms

## Detailed patterns and worked examples

Detailed pattern documentation lives in `references/details.md`. Read that file when the navigation tier above is insufficient.

## Best Practices Summary

1. **Always use strict mode** - `set -Eeuo pipefail`
2. **Quote all variables** - `"$variable"` prevents word splitting
3. **Use [[]] conditionals** - More robust than [ ]
4. **Implement error trapping** - Catch and handle errors gracefully
5. **Validate all inputs** - Check file existence, permissions, formats
6. **Use functions for reusability** - Prefix with meaningful names
7. **Implement structured logging** - Include timestamps and levels
8. **Support dry-run mode** - Allow users to preview changes
9. **Handle temporary files safely** - Use mktemp, cleanup with trap
10. **Design for idempotency** - Scripts should be safe to rerun
11. **Document requirements** - List dependencies and minimum versions
12. **Test error paths** - Ensure error handling works correctly
13. **Use `command -v`** - Safer than `which` for checking executables
14. **Prefer printf over echo** - More predictable across systems