Chuyển đến nội dung
Skillpedia
Lập trình chung41k · #6783

bash-defensive-patterns

Skill này làm gì

Hướng dẫn này chỉ cách viết các script tự động hóa (Bash) an toàn và đáng tin cậy hơn. Nó giúp ngăn các lỗi thường gặp và đảm bảo script hoạt động đúng ngay cả trong các tình huống bất ngờ.

Khi nào nên dùng

  • ✓Khi bạn tạo script tự động hóa các tác vụ quan trọng tại nơi làm việc.
  • ✓Khi bạn xây dựng các bước tự động hóa cho quy trình phát triển (CI/CD).
  • ✓Khi bạn phát triển công cụ hệ thống cần rất đáng tin cậy.

Liên kết skill

https://github.com/wshobson/agents/blob/main/plugins/shell-scripting/skills/bash-defensive-patterns/SKILL.md

Sao chép rồi dán vào AI của bạn (Claude, ChatGPT hoặc Cursor) và nhờ nó làm theo hướng dẫn.

Cách dùng

Cách dễ nhất: sao chép tin nhắn và dán vào AI của bạn. AI sẽ đọc skill và làm theo hướng dẫn.

Tin nhắn có sẵn để dán

Hãy dùng skill này để giúp tôi: https://github.com/wshobson/agents/blob/main/plugins/shell-scripting/skills/bash-defensive-patterns/SKILL.md Đọc tệp SKILL.md tại liên kết đó và làm theo hướng dẫn cho việc của tôi.
Bạn muốn AI tự tìm những skill như thế này?Cài MCP Skillpedia miễn phí: chỉ một dòng, và AI sẽ tìm trong 15.000+ skill trước mỗi việc.Cài đặt trong 1 phút →

Ví dụ cách nhờ AI

“Làm sao để viết một script Bash không bị lỗi nếu một file không tồn tại?”

5 skill mới mỗi tuần. Đọc chỉ 1 phút.

Cách hoạt động

Hướng dẫn dùng các kỹ thuật như bật chế độ an toàn (`set -euo pipefail`), luôn đặt biến trong dấu ngoặc kép (`"$variable"`), và kiểm tra xem lệnh đã thực thi thành công hay chưa.

Nội dung kỹ thuật (SKILL.md gốc)

Lấy nguyên văn từ GitHub, do tác giả viết bằng tiếng Anh.

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
Câu hỏi thường gặp

“Strict mode” là gì?

Đó là bộ quy tắc (`set -Eeuo pipefail`) làm script nghiêm ngặt hơn, buộc sửa các lỗi thường gặp mà nếu không sẽ bị bỏ qua.

Tại sao nên đặt biến trong dấu ngoặc kép?

Đặt dấu ngoặc kép quanh biến (`"$variable"`) ngăn script hiểu sai khoảng trắng hoặc ký tự đặc biệt trong giá trị của biến, nhờ đó tránh lỗi.

“Idempotency” nghĩa là gì?

Một script idempotent có thể chạy nhiều lần mà không gây tác dụng phụ ngoài ý muốn. Nó đảm bảo kết quả cuối cùng giống nhau, bất kể script được chạy bao nhiêu lần.

Chi tiết

Mô tả gốc của tác giả

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.

repo: wshobson/agents

Độ phổ biến

Sao GitHub
41k
Vị trí xếp hạng
#6783
Giấy phép
MIT
Cập nhật
15/10/2026

Skill này có hữu ích không?

0

Đăng nhập để bình chọn.

Bình luận

  • …

Thêm skill từ kho mã này

Skill tương tự