agentleFS
hellyguohellyguo/self-ai-spec/skills/shell-check/SKILL.md

shell-check

Skill8 starsChanged 17 days agoApache-2.0View on GitHub

使用 ShellCheck 对 Shell 脚本做静态分析,检测语法错误、潜在缺陷与风格问题。适用于 Shell 脚本质量检查、CI 门禁、脚本移植前排查。

The file

Files
SKILL.md

ShellCheck Shell脚本静态分析技能

本文件中的 templates/... 均指本技能目录下的 templates/ 子目录。

工具位置

SHELLCHECK=/usr/bin/shellcheck
# 版本: ShellCheck 0.10.0

执行流程

步骤1:确定脚本文件

SCRIPT_FILE=script.sh  # 单个文件
SCRIPT_DIR=scripts/    # 目录下的所有脚本

步骤2:执行分析

# 基础检查
$SHELLCHECK script.sh

# 检查目录下所有脚本
$SHELLCHECK scripts/*.sh

# 指定Shell方言
$SHELLCHECK --shell=bash script.sh
$SHELLCHECK --shell=sh script.sh
$SHELLCHECK --shell=dash script.sh
$SHELLCHECK --shell=ksh script.sh

步骤3:指定输出格式

$SHELLCHECK --format=json script.sh > shellcheck.json
$SHELLCHECK --format=json1 script.sh > shellcheck-detail.json
$SHELLCHECK --format=checkstyle script.sh > shellcheck.xml
$SHELLCHECK --format=gcc script.sh
$SHELLCHECK --format=quiet script.sh

步骤4:生成报告

使用模板 templates/report.md 输出分析报告。

常用参数

参数描述示例
-s, --shell指定Shell方言--shell=bash
-S, --severity最小严重级别--severity=error
-e, --exclude排除特定警告-e SC2086,SC2034
-i, --include只包含特定警告-i SC2086,SC2034
-a, --check-sourced检查source的文件--check-sourced
-x, --external-sources允许外部source--external-sources
-o, --enable启用可选检查--enable=all
-f, --format输出格式--format=json

严重级别

级别描述
error错误,必须修复
warning警告,建议修复
info信息,可选修复
style风格,可选修复

排除警告

行内排除

# shellcheck disable=SC2086
echo $var

# shellcheck disable=SC2086,SC2034
echo $var

# shellcheck disable-next=SC2086
echo $var

命令行排除

$SHELLCHECK -e SC2086,SC2034 script.sh
$SHELLCHECK -i SC2086 script.sh

配置文件(.shellcheckrc)

disable=SC2086
shell=bash
source-path=SCRIPTDIR
enable=require-variable-braces

常用可选检查

检查项描述
require-variable-braces要求变量使用大括号
add-default-casecase语句需要默认分支
quote-safe-variables引用安全的变量
useless-use-of-cat检测无用的cat使用

典型使用场景

场景命令
快速检查$SHELLCHECK --severity=error script.sh
完整检查$SHELLCHECK --enable=all --severity=style script.sh
CI集成$SHELLCHECK --format=gcc --severity=warning scripts/*.sh
检查sourced文件$SHELLCHECK --check-sourced --source-path=scripts script.sh

最佳实践

  1. 始终用双引号引用变量
  2. 优先使用[[ ]]而非[ ]
  3. 优先使用$( )而非反引号
  4. 检查cd等命令的返回值
  5. 脚本开头设置set -e

技能版本: 1.0.0 工具版本: ShellCheck 0.10.0 适用项目: Shell脚本(bash/sh/dash/ksh)

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.