基础依赖与工具检查
检查新机器常用运维工具与编译依赖是否齐全,输出缺口和对应发行版的安装命令
新机器初始化时经常漏装工具,等到需要时才发现 curl、vim、tar、jq 都没有。这个脚本把常用工具和编译依赖逐项检查一遍,缺什么、怎么装一目了然。
只读检查,不安装任何软件包;输出可以直接粘贴给包管理器。
配置参数并生成脚本
选参数 → 即时生成 → 复制调用命令。绿色组合经过构建期校验,黄色为自定义组合。
#!/bin/sh
# ==============================================================================
# Linux脚本库 · 基础依赖与工具检查
# ------------------------------------------------------------------------------
# 脚本 ID : system-deps-check
# 版本 : 0.1.0
# 构建标识 : 4d3f63a7
# 分类 : system / deps
# 危险等级 : 只读(不修改系统)
# 幂等 : 是(重复执行无副作用)
# 预演 : 不支持
# 默认参数 : format=text
# 参数覆盖 : 在命令后加 --<参数名> <值>,例如 --format json;--help 查看全部
# 文档 : https://linuxmirror.cn/script/system-deps-check/
# ------------------------------------------------------------------------------
# 本文件由 Linux脚本库生成器输出,请勿手工修改。
# 执行前请阅读文档,并确认已理解脚本将要做的改动。
# ==============================================================================
set -eu
umask 022
# ---- 脚本元信息 ----
LM_SCRIPT_ID='system-deps-check'
LM_SCRIPT_VERSION='0.1.0'
LM_SCRIPT_BUILD='4d3f63a7'
LM_DOC_URL='https://linuxmirror.cn/script/system-deps-check/'
# ---- 公共函数 ----
die() {
_lm_code=$1
shift
printf '[错误] %s\n' "$*" >&2
exit "$_lm_code"
}
log() {
printf '[信息] %s\n' "$*"
}
warn() {
printf '[警告] %s\n' "$*" >&2
}
has_cmd() {
command -v "$1" >/dev/null 2>&1
}
need_cmd() {
for _lm_c in "$@"; do
has_cmd "$_lm_c" || die 3 "缺少依赖命令: $_lm_c"
done
}
need_root() {
[ "$(id -u)" -eq 0 ] || die 4 '需要 root 权限,请使用 sudo 执行'
}
need_os_release() {
[ -r /etc/os-release ] || die 3 '/etc/os-release 不存在,无法识别系统发行版'
}
in_container() {
[ -f /.dockerenv ] || [ -f /run/.containerenv ]
}
has_systemd() {
[ -d /run/systemd/system ]
}
# ---- 默认参数(可用命令行覆盖)----
LM_FORMAT='text'
# ---- 命令行参数(覆盖上面的默认值)----
lm_usage() {
cat <<'LM_USAGE'
用法: bash <(curl -sSL "https://linuxmirror.cn/s/system-deps-check.sh") [选项]
选项:
-format, --format 输出格式(可选: text|json)
-h, --help 显示本帮助
--version 显示脚本版本
LM_USAGE
}
LM_EXTRA_ARGS=""
while [ $# -gt 0 ]; do
case "$1" in
-h|--help)
lm_usage
exit 0
;;
--version)
printf '%s\n' "$LM_SCRIPT_ID v$LM_SCRIPT_VERSION"
exit 0
;;
--)
shift
break
;;
-format|--format)
[ $# -ge 2 ] || die 2 'format 需要一个值'
LM_FORMAT=$2
shift 2
;;
--format=*|-format=*)
LM_FORMAT=${1#*=}
shift
;;
*) LM_EXTRA_ARGS="$LM_EXTRA_ARGS $1"; shift ;;
esac
done
# 未识别的参数留给正文(例如 --dry-run);正文可用 "$@" 读取
if [ -n "$LM_EXTRA_ARGS" ]; then
# shellcheck disable=SC2086
set -- $LM_EXTRA_ARGS
fi
# ---- 参数兜底校验 ----
case $LM_FORMAT in
text|json) ;;
*) printf '[错误] %s 取值非法: %s\n' '输出格式' "$LM_FORMAT" >&2; exit 2 ;;
esac
# ==============================================================================
# 正文
# ==============================================================================
# ---- 工具函数 ----
if has_cmd apt-get; then
LM_PM=apt-get
elif has_cmd dnf; then
LM_PM=dnf
elif has_cmd yum; then
LM_PM=yum
elif has_cmd apk; then
LM_PM=apk
elif has_cmd pacman; then
LM_PM=pacman
else
LM_PM=unknown
fi
REQUIRED='curl wget tar gzip unzip ss awk sed grep find ps df'
OPTIONAL='git make gcc g++ jq rsync vim tmux lsof iotop sysstat htop openssl nc'
missing=''
for tool in $REQUIRED; do
has_cmd "$tool" || missing="$missing $tool"
done
optional_missing=''
for tool in $OPTIONAL; do
has_cmd "$tool" || optional_missing="$optional_missing $tool"
done
if [ "$LM_FORMAT" = 'json' ]; then
printf '{"package_manager":"%s","kernel":"%s","required_missing":"%s","optional_missing":"%s"}\n' \
"$LM_PM" "$(uname -r)" "$(printf '%s' "$missing" | sed 's/^[[:space:]]*//')" "$(printf '%s' "$optional_missing" | sed 's/^[[:space:]]*//')"
exit 0
fi
log '================ 基础依赖检查 ================'
log "包管理器 : $LM_PM"
log "内核 : $(uname -s) $(uname -r)"
log ''
log '--- 排障必备 ---'
for tool in $REQUIRED; do
if has_cmd "$tool"; then
printf ' %-10s OK\n' "$tool"
else
printf ' %-10s MISSING\n' "$tool"
fi
done
log ''
log '--- 常用工具(按需)---'
for tool in $OPTIONAL; do
if has_cmd "$tool"; then
printf ' %-10s OK\n' "$tool"
else
printf ' %-10s MISSING\n' "$tool"
fi
done
if [ -n "$missing" ]; then
log ''
warn "缺少必备工具:$missing"
case "$LM_PM" in
apt-get) log "安装: apt-get update && apt-get install -y$missing" ;;
dnf|yum) log "安装: $LM_PM install -y$missing" ;;
apk) log "安装: apk add$missing" ;;
pacman) log "安装: pacman -S --noconfirm$missing" ;;
*) warn '未知包管理器,请手动安装缺失工具' ;;
esac
else
log ''
log '必备工具齐全'
fi
775fb6b601a0c5d47622e984c39c95866b30c56d20a96cdc2b585a79216e249b# 唯一支持的调用方式:固定 URL 是静态脚本,参数在命令后覆盖
bash <(curl -sSL "https://linuxmirror.cn/s/system-deps-check.sh")
# 查看全部参数
bash <(curl -sSL "https://linuxmirror.cn/s/system-deps-check.sh") --helphttps://linuxmirror.cn/s/system-deps-check.shURL 永远不变(版本升级也继续可用);只允许 curl 调用,浏览器与 wget 会被拒绝。
兼容矩阵
共支持 8 个发行版,均为理论支持。 系统识别统一读取 /etc/os-release, 不依赖 /etc/redhat-release。
| 发行版 | 最低版本 | 最高版本 | 实测 |
|---|---|---|---|
| Debian | 11 | 13 | 未实测 |
| Ubuntu | 20.04 | 26.04 | 未实测 |
| CentOS | 7 | 9 | 未实测 |
| Rocky Linux | 8 | 10 | 未实测 |
| AlmaLinux | 8 | 10 | 未实测 |
| openEuler | 20.03 | — | 未实测 |
| Alpine | 3 | — | 未实测 |
| Arch Linux | — | — | 未实测 |
支持架构:x86_64 · ARM64 (aarch64)
运行要求与影响范围
- 运行权限
- 普通用户即可
- 网络依赖
- 无需联网
- 幂等性
- 幂等,重复执行无副作用
- 预演支持
- 不支持预演
常见问题
会安装软件包吗?
不会。脚本只检查命令是否存在并打印建议的安装命令,是否安装由你决定。
为什么有些工具标记为可选?
curl/wget/tar/gzip/ps 属于排障必备;gcc/make/git/jq 等属于按需安装,缺失不影响基础使用。
同类脚本
免责声明:本脚本按"原样"提供,作者与本站不对因使用脚本造成的任何直接或间接损失负责。 请在测试环境验证后再用于生产,并自行承担执行风险。脚本由服务端按参数实时渲染, 可在页面生成器里核对完整内容后,再通过固定 URL 执行。 如发现安全问题请通过 dakerteam@163.com 联系。
固定地址(版本升级不变,参数用 query):https://linuxmirror.cn/s/system-deps-check.sh· sha256 端点:https://linuxmirror.cn/s/system-deps-check.sha256
审计用不可变地址(内容变更即换地址,适合复现与比对):https://linuxmirror.cn/s/system-deps-check/0.1.0/