
关于
Linux、macOS 和 Windows 的操作系统和 Shell 脚本故障排除工作流。涵盖 Bash 脚本、PowerShell 和跨平台脚本编写。
name: os-scripting description: "适用于 Linux、macOS 和 Windows 的操作系统与 Shell 脚本故障排除工作流。涵盖 bash 脚本编写、系统管理、调试和自动化。" category: workflow-bundle risk: safe source: personal date_added: "2026-02-27"
操作系统/Shell 脚本故障排除工作流包
概述
适用于 Linux、macOS 和 Windows 的操作系统故障排除、Shell 脚本编写和系统管理综合工作流。此工作流包协调各项技能,用于调试系统问题、创建健壮脚本和自动化管理任务。
何时使用此工作流
在以下情况下使用此工作流:
- 调试 Shell 脚本错误
- 创建生产级 bash 脚本
- 排除系统故障
- 自动化系统管理任务
- 管理进程和服务
- 配置系统资源
工作流阶段
阶段 1:环境评估
调用的技能
bash-linux- Linux bash 模式bash-pro- 专业 bash 脚本编写bash-defensive-patterns- 防御性脚本编写
操作步骤
- 识别操作系统和版本
- 检查可用工具和命令
- 验证权限和访问
- 评估系统资源
- 查看日志和错误信息
诊断命令
# System information
uname -a
cat /etc/os-release
hostnamectl
# Resource usage
top
htop
df -h
free -m
# Process information
ps aux
pgrep -f pattern
lsof -i :port
# Network status
netstat -tulpn
ss -tulpn
ip addr show
复制粘贴提示
Use @bash-linux to diagnose system performance issues
阶段 2:脚本分析
调用的技能
bash-defensive-patterns- 防御性脚本编写shellcheck-configuration- ShellCheck 代码检查bats-testing-patterns- Bats 测试
操作步骤
- 运行 ShellCheck 进行代码检查
- 分析脚本结构
- 识别潜在问题
- 检查错误处理
- 验证变量使用
ShellCheck 使用方法
# Install ShellCheck
sudo apt install shellcheck # Debian/Ubuntu
brew install shellcheck # macOS
# Run ShellCheck
shellcheck script.sh
shellcheck -f gcc script.sh
# Fix common issues
# - Use quotes around variables
# - Check exit codes
# - Handle errors properly
复制粘贴提示
Use @shellcheck-configuration to lint and fix shell scripts
阶段 3:调试
调用的技能
systematic-debugging- 系统化调试debugger- 调试专家error-detective- 错误模式检测
操作步骤
- 启用调试模式
- 添加日志语句
- 追踪执行流程
- 隔离失败部分
- 逐个测试组件
调试技术
# Enable debug mode
set -x # Print commands
set -e # Exit on error
set -u # Exit on undefined variable
set -o pipefail # Pipeline failure detection
# Add logging
log() {
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $*" >> /var/log/script.log
}
# Trap errors
trap 'echo "Error on line $LINENO"' ERR
# Test sections
bash -n script.sh # Syntax check
bash -x script.sh # Trace execution
复制粘贴提示
Use @systematic-debugging to trace and fix shell script errors
阶段 4:脚本开发
调用的技能
bash-pro- 专业脚本编写bash-defensive-patterns- 防御性模式linux-shell-scripting- Shell 脚本编写
操作步骤
- 设计脚本结构
- 实现函数
- 添加错误处理
- 包含输入验证
- 添加帮助文档
脚本模板
#!/usr/bin/env bash
set -euo pipefail
# Constants
readonly SCRIPT_NAME=$(basename "$0")
readonly SCRIPT_DIR=$(cd "$(dirname "$0")" && pwd)
# Logging
log() {
local level="$1"
shift
echo "[$(date '+%Y-%m-%d %H:%M:%S')] [$level] $*" >&2
}
info() { log "INFO" "$@"; }
warn() { log "WARN" "$@"; }
error() { log "ERROR" "$@"; exit 1; }
# Usage
usage() {
cat <<EOF
Usage: $SCRIPT_NAME [OPTIONS]
Options:
-h, --help Show this help message
-v, --verbose Enable verbose output
-d, --debug Enable debug mode
Examples:
$SCRIPT_NAME --verbose
$SCRIPT_NAME -d
EOF
}
# Main function
main() {
local verbose=false
local debug=false
while [[ $# -gt 0 ]]; do
case "$1" in
-h|--help)
usage
exit 0
;;
-v|--verbose)
verbose=true
shift
;;
-d|--debug)
debug=true
set -x
shift
;;
*)
error "Unknown option: $1"
;;
esac
done
info "Script started"
# Your code here
info "Script completed"
}
main "$@"
复制粘贴提示
Use @bash-pro to create a production-ready backup script
Use @linux-shell-scripting to automate system maintenance tasks
阶段 5:测试
兼容工具
Claude CodeCursor
标签
AI与机器学习