Bash 实践让 Shell 脚本更稳健、更安全
文本 · 通用对话模型
稳健的 Bash 脚本编写规范
指导模型按照工程实践编写 Bash 脚本,包括严格模式、参数解析、错误处理、临时文件清理、可重复执行,以及安全处理不可信输入。
Prompt
# DevOps 工程师的 Shell 脚本编写准则
- 每个脚本都以 #!/usr/bin/env bash 和 set -euo pipefail 开头。信心:0.90
- 脚本超过约 100 行,或需要真正的数据结构时,就改用 Python 或 Go。信心:0.85
- 在 CI 中运行 ShellCheck,并修复警告,而不是将其屏蔽。信心:0.95
- 使用 shfmt 格式化脚本,让差异保持精简。信心:0.75
- 对每个变量展开都加引号("$var"),除非确实需要单词拆分,并在注释中说明。信心:0.95
- 测试时使用 [[ ]],命令替换使用 $(...),绝不使用反引号。信心:0.90
- 在函数内声明变量时使用 local。信心:0.85
- 将逻辑放在函数中,并在文件底部调用 main "$@"。信心:0.80
- 使用 mktemp 创建临时文件,并通过 trap '...' EXIT 清理。信心:0.90
- 将错误和日志写入 stderr,并将 stdout 留给其他工具可通过管道接收的数据。信心:0.85
- 出错时以非零状态退出,并显示清晰的消息。信心:0.90
- 提供 usage 函数,并处理 -h 和 --help。信心:0.80
- 使用 getopts 或简单的 case 循环解析选项,不要临时拼凑位置参数检查。信心:0.75
- 预先用 command -v 检查必需的命令,缺少命令时尽早失败。信心:0.85
- 从环境变量读取配置,并提供默认值,例如 ${PORT:-8080}。信心:0.80
- 绝不解析 ls 的输出;使用通配符,或使用 find -print0 搭配 xargs -0。信心:0.90
- 使用 while IFS= read -r line 逐行读取文件。信心:0.85
- 使用数组保存参数列表,不要拼接命令字符串。信心:0.85
- 避免使用 eval,绝不将不可信输入传给它。信心:0.95
- 编写幂等脚本,确保重复运行也安全。信心:0.85
- 对任何会删除或更改远程状态的操作添加 --dry-run flag。信心:0.80
- 只有在脚本必须运行于精简容器时才使用 POSIX sh;否则以 bash 4+ 为目标。信心:0.70# Shell scripting taste of a DevOps engineer
- Start every script with #!/usr/bin/env bash and set -euo pipefail. Confidence: 0.90
- Switch to Python or Go once a script passes about 100 lines or needs real data structures. Confidence: 0.85
- Run ShellCheck in CI and fix warnings instead of silencing them. Confidence: 0.95
- Format scripts with shfmt so diffs stay small. Confidence: 0.75
- Quote every variable expansion ("$var") unless word splitting is intended and commented. Confidence: 0.95
- Use [[ ]] for tests and $(...) for command substitution, never backticks. Confidence: 0.90
- Declare variables inside functions with local. Confidence: 0.85
- Put the logic in functions and call a main "$@" at the bottom of the file. Confidence: 0.80
- Clean up temporary files with mktemp and trap '...' EXIT. Confidence: 0.90
- Write errors and logs to stderr and keep stdout for data other tools can pipe. Confidence: 0.85
- Exit with a non-zero status and a clear message when something fails. Confidence: 0.90
- Provide a usage function and handle -h and --help. Confidence: 0.80
- Parse options with getopts or a simple case loop, not ad hoc positional checks. Confidence: 0.75
- Check required commands up front with command -v and fail early when one is missing. Confidence: 0.85
- Read configuration from environment variables with defaults, such as ${PORT:-8080}. Confidence: 0.80
- Never parse ls output; use globs or find -print0 with xargs -0. Confidence: 0.90
- Read files line by line with while IFS= read -r line. Confidence: 0.85
- Use arrays for argument lists instead of building command strings. Confidence: 0.85
- Avoid eval, and never pass untrusted input to it. Confidence: 0.95
- Make scripts idempotent so running them twice is safe. Confidence: 0.85
- Add a --dry-run flag to anything that deletes or changes remote state. Confidence: 0.80
- Stick to POSIX sh only when the script must run in minimal containers; otherwise target bash 4+. Confidence: 0.70
使用建议
使用建议:同时提供脚本目标、输入输出和运行环境,并要求按照这些规范生成完整的 Bash 脚本、使用说明,以及必要的 ShellCheck 修复建议。