一键脚本
# Gitee 源(推荐,国内访问快)
bash <(curl -sSL gitee.com/meimolihan/cmdbox/raw/master/sh/install-check.sh)
# cmdbox 站点
bash <(curl -sSL ../sh/install-check.sh)
需要 root 权限:非 root 用户请执行
sudo bash -c "$(curl -sSL gitee.com/meimolihan/cmdbox/raw/master/sh/install-check.sh)",或先下载再sudo bash install-check.sh。
补充说明
install-check.sh 是一个安装器:它会自动判断当前系统是否为 iStoreOS,下载对应版本的查看脚本到 /etc/profile.d/,并创建快捷命令 g。安装完成后,在终端直接输入 g 即可查看系统信息。
| 判断结果 | 下载脚本 | 安装位置 | 快捷命令 |
|---|---|---|---|
iStoreOS(ID=istoreos) |
istoreos-check.sh |
/etc/profile.d/linux-check.sh |
/usr/local/bin/g |
| 其它 Linux | linux-check.sh |
/etc/profile.d/linux-check.sh |
/usr/local/bin/g |
两个版本最终都保存为
/etc/profile.d/linux-check.sh,只是内容不同;因此g up更新时也统一操作该文件。
安装流程
- 读取
/etc/os-release,判断ID是否为istoreos - 若存在旧的
/etc/profile.d/istoreos-check.sh则删除 - 从 Gitee 下载对应脚本到
/etc/profile.d/linux-check.sh chmod +x赋予执行权限- 创建软链接
/usr/local/bin/g(失败时回退并提示手动路径) - 检查
/usr/local/bin是否在PATH中 - 立即执行一次脚本,显示系统信息
两个版本的功能差异
| 项目 | linux-check.sh(通用 Linux) |
istoreos-check.sh(iStoreOS) |
|---|---|---|
| 版本变量 | SH_VERSION(当前 v1.0.3) |
sh_v(当前 v1.1.2) |
| 主机名称 | hostname |
/proc/sys/kernel/hostname |
| 内网 IP | hostname -I / ip -o -4 addr |
ifconfig br-lan |
| 默认网关 | ip route,回退 route -n |
ip route |
| 版本提示 | 非交互执行时检测新版本 | 非交互执行时检测新版本(卡片式排版) |
| 重复显示控制 | 交互式 shell 仅首次显示(LINUX_CHECK_SHOWN) |
同上 |
| 更新动作 | 执行 meimolihan/script 仓库的安装器 |
执行 cmdbox 仓库的 istoreos-check.sh |
输出字段
| 字段 | 说明 |
|---|---|
| 主机名称 | 当前主机名 |
| 内核版本 | uname -r |
| CPU 架构 | uname -m(x86_64、aarch64 等) |
| CPU 占用 | 由 top 实时计算,缺失时回退 mpstat(通用版) |
| IPV4内网 | 内网 IPv4 地址 |
| 默认网关 | 默认路由网关 |
| 磁盘占用 | 根分区 已用/总量 (百分比) |
| 运行时间 | 自开机以来的时与分 |
常用命令
g # 显示系统信息
g up # 更新脚本到最新版
g version # 显示当前版本并检查更新
g help # 显示帮助信息
卸载脚本
rm -f /usr/local/bin/g /etc/profile.d/linux-check.sh /etc/profile.d/istoreos-check.sh
# 已打开的终端执行 hash -r,或重新登录使命令失效
注意事项
- 依赖
curl下载脚本,需能访问 Gitee 或cmdbox.meimolihan.eu.org - 两个版本最终都落到
/etc/profile.d/linux-check.sh,仅内容不同,勿重复手动放置 g up的升级源与首次安装源不同:通用版调用meimolihan/script仓库安装器,iStoreOS 版调用cmdbox仓库的istoreos-check.sh,请保证对应仓库可访问- iStoreOS 版取 IP 依赖
ifconfig br-lan(BusyBox 默认自带);通用版优先hostname -I - 即使系统是 OpenWrt,只要
/etc/os-release中ID不为istoreos,仍会安装通用版 - 下载失败或文件为空时安装器会报错返回,不会强行创建
g命令 - 脚本写入
/etc/profile.d/后随登录 shell 生效;当前终端可执行source /etc/profile或hash -r
脚本源码
安装器 install-check.sh
#!/bin/bash
gl_hui='\e[37m'
gl_hong='\033[31m'
gl_lv='\033[32m'
gl_huang='\033[33m'
gl_lan='\033[34m'
gl_bai='\033[0m'
gl_zi='\033[35m'
gl_bufan='\033[96m'
sh_download() {
local script_path="/etc/profile.d/linux-check.sh"
local link_path="/usr/local/bin/g"
if [ -s /etc/os-release ]; then
. /etc/os-release
case "$ID" in
istoreos) ISTOREOS=1 ;;
*) ISTOREOS=0 ;;
esac
else
ISTOREOS=0
fi
if [ "$ISTOREOS" = 1 ]; then
echo "检测到 iStoreOS 系统"
else
echo "检测到非 iStoreOS 系统"
fi
if [ "$ISTOREOS" = 1 ]; then
URL="https://gitee.com/meimolihan/cmdbox/raw/master/sh/istoreos-check.sh"
else
URL="https://gitee.com/meimolihan/cmdbox/raw/master/sh/linux-check.sh"
fi
if [ -f "/etc/profile.d/istoreos-check.sh" ]; then
echo -e "清理旧别名文件 ${gl_hong}.${gl_huang}.${gl_lv}.${gl_bai}"
rm -f /etc/profile.d/istoreos-check.sh
fi
echo -e "正在下载脚本 ${gl_hong}.${gl_huang}.${gl_lv}.${gl_bai}"
if ! curl -sS -o "$script_path" "$URL"; then
echo "错误:下载失败,请检查网络连接"
return 1
fi
if [ ! -s "$script_path" ]; then
echo "错误:下载的文件为空或无效"
return 1
fi
chmod +x "$script_path"
echo "脚本已保存到: $script_path"
echo -e "创建快捷命令 ${gl_hong}.${gl_huang}.${gl_lv}.${gl_bai}"
if [ -L "$link_path" ] || [ -f "$link_path" ]; then
rm -f "$link_path"
fi
if ln -sf "$script_path" "$link_path"; then
echo "快捷命令已创建: 'g' -> $script_path"
else
echo -e "警告:创建软链接失败,尝试其他路径 ${gl_hong}.${gl_huang}.${gl_lv}.${gl_bai}"
if ln -sf "$script_path" "/usr/local/bin/g"; then
link_path="/usr/local/bin/g"
echo "快捷命令已创建: '/usr/local/bin/g' -> $script_path"
else
echo "错误:无法创建软链接,请手动执行: $script_path"
fi
fi
local link_dir=$(dirname "$link_path")
if ! echo "$PATH" | tr ':' '\n' | grep -q "^$(echo "$link_dir" | sed 's/[\/&]/\\&/g')$"; then
echo "注意:$link_dir 不在 PATH 中,可能需要添加到 PATH 或使用完整路径"
else
echo "检查: 'g' 命令已在 PATH 中"
fi
echo -e "\n测试快捷命令 ${gl_hong}.${gl_huang}.${gl_lv}.${gl_bai}"
if command -v g >/dev/null 2>&1; then
echo "✓ 快捷命令 'g' 已可用"
else
echo "⚠ 'g' 命令未找到,可能需要重新登录或执行: source /etc/profile"
echo " 或者直接使用: $script_path"
fi
echo -e "\n执行脚本 ${gl_hong}.${gl_huang}.${gl_lv}.${gl_bai}"
if [ -x "$script_path" ]; then
"$script_path" "$@"
else
echo "错误:脚本不可执行"
return 1
fi
}
if [ "${BASH_SOURCE[0]}" = "$0" ]; then
sh_download "$@"
fi
通用 Linux 版 linux-check.sh
#!/bin/bash
SH_VERSION="1.0.3"
gl_hui='\e[37m'
gl_hong='\033[31m'
gl_lv='\033[32m'
gl_huang='\033[33m'
gl_lan='\033[34m'
gl_bai='\033[0m'
gl_zi='\033[35m'
gl_bufan='\033[96m'
log_info() { echo -e "${gl_lan}[信息]${gl_bai} $*"; }
log_ok() { echo -e "${gl_lv}[成功]${gl_bai} $*"; }
log_warn() { echo -e "${gl_huang}[警告]${gl_bai} $*"; }
log_error() { echo -e "${gl_hong}[错误]${gl_bai} $*" >&2; }
break_end() {
echo -e "${gl_lv}操作完成${gl_bai}"
echo -e "${gl_bai}按任意键继续 ${gl_hong}.${gl_huang}.${gl_lv}.${gl_bai}\c"
read -r -n 1 -s -r -p ""
echo ""
clear
}
exit_script() {
clear
exit 0
}
handle_invalid_input() {
echo -ne "\r${gl_huang}无效的输入,请重新输入! ${gl_zi}1${gl_huang}秒后返回"
sleep 1
echo -e "\r${gl_lv}无效的输入,请重新输入! ${gl_zi}0${gl_lv}秒后返回"
sleep 0.5
return 2
}
get_remote_version() {
local remote_version
# 使用多个源,提高成功率
remote_version=$(curl -s --connect-timeout 5 \
https://gitee.com/meimolihan/cmdbox/raw/master/sh/linux-check.sh \
| grep -o 'SH_VERSION="[0-9.]*"' 2>/dev/null | head -1 | cut -d'"' -f2)
if [ -z "$remote_version" ]; then
remote_version=$(curl -s --connect-timeout 5 \
https://cmdbox.meimolihan.eu.org/sh/linux-check.sh \
| grep -o 'SH_VERSION="[0-9.]*"' 2>/dev/null | head -1 | cut -d'"' -f2)
fi
echo "$remote_version"
}
check_for_update() {
local remote_version
remote_version=$(get_remote_version)
if [ -z "$remote_version" ]; then
return 1
fi
if [ "$SH_VERSION" != "$remote_version" ]; then
echo -e "\n${gl_hong}🎉 发现新版本!${gl_bai}"
echo -e "${gl_bufan}————————————————————————${gl_bai}"
echo -e " 当前版本: ${gl_huang}v$SH_VERSION${gl_bai}"
echo -e " 最新版本: ${gl_lv}v$remote_version${gl_bai}"
echo -e "${gl_bufan}————————————————————————${gl_bai}"
echo -e " ${gl_bai}输入命令: ${gl_huang}g up${gl_bai} 更新到最新版"
echo -e "${gl_bufan}————————————————————————${gl_bai}\n"
return 0
fi
return 1
}
get_local_ip() {
local ip
ip=$(hostname -I 2>/dev/null | awk '{print $1}' | head -n1)
if [ -z "$ip" ]; then
ip=$(ip -o -4 addr show scope global 2>/dev/null | awk '{print $4}' | cut -d'/' -f1 | head -n1)
fi
[ -n "$ip" ] && echo "$ip" || echo "无法获取IP"
}
get_cpu_usage() {
local cpu_usage
cpu_usage=$(top -bn1 2>/dev/null | grep "Cpu(s)" | awk '{printf "%.2f%%", 100 - $8}')
if [ -z "$cpu_usage" ]; then
cpu_usage=$(mpstat 1 1 2>/dev/null | tail -1 | awk '{printf "%.2f%%", 100 - $NF}')
fi
[ -n "$cpu_usage" ] && echo "$cpu_usage" || echo "无法获取"
}
get_uptime() {
local uptime_seconds hours minutes
uptime_seconds=$(awk '{print int($1)}' /proc/uptime 2>/dev/null)
if [ -n "$uptime_seconds" ]; then
hours=$((uptime_seconds / 3600))
minutes=$(((uptime_seconds % 3600) / 60))
echo "${hours}时${minutes}分"
else
uptime_seconds=$(uptime -p 2>/dev/null | sed 's/up //')
[ -n "$uptime_seconds" ] && echo "$uptime_seconds" || echo "无法获取"
fi
}
get_default_gateway() {
local gateway
gateway=$(ip route show default 2>/dev/null | awk '/default/ {print $3}' | head -n1)
if [ -z "$gateway" ]; then
gateway=$(route -n 2>/dev/null | awk '$1 == "0.0.0.0" {print $2}' | head -n1)
fi
[ -n "$gateway" ] && echo "$gateway" || echo "无法获取"
}
get_disk_usage() {
local disk_info
disk_info=$(df -h / 2>/dev/null | awk 'NR==2 {printf "%s/%s (%s)", $3, $2, $5}')
if [ -z "$disk_info" ]; then
disk_info=$(df -h . 2>/dev/null | awk 'NR==2 {printf "%s/%s (%s)", $3, $2, $5}')
fi
[ -n "$disk_info" ] && echo "$disk_info" || echo "无法获取"
}
show_system_info() {
clear
echo -e "${gl_zi}>>> 系统信息${gl_bai}"
# echo -e "${gl_lv}💡 输入 ${gl_huang}g up${gl_lv} 可更新脚本${gl_bai}"
echo -e "${gl_bufan}————————————————————————${gl_bai}"
echo -e "${gl_bufan}主机名称 : ${gl_bai}$(hostname 2>/dev/null || echo "未知")"
echo -e "${gl_bufan}内核版本 : ${gl_bai}$(uname -r 2>/dev/null || echo "未知")"
echo -e "${gl_bufan}————————————————————————${gl_bai}"
echo -e "${gl_bufan}CPU 架构 : ${gl_bai}$(uname -m 2>/dev/null || echo "未知")"
echo -e "${gl_bufan}CPU 占用 : ${gl_bai}$(get_cpu_usage)"
echo -e "${gl_bufan}————————————————————————${gl_bai}"
echo -e "${gl_bufan}IPV4内网 : ${gl_bai}$(get_local_ip)"
echo -e "${gl_bufan}默认网关 : ${gl_bai}$(get_default_gateway)"
echo -e "${gl_bufan}————————————————————————${gl_bai}"
echo -e "${gl_bufan}磁盘占用 : ${gl_bai}$(get_disk_usage)"
echo -e "${gl_bufan}运行时间 : ${gl_bai}$(get_uptime)"
echo -e "${gl_bufan}————————————————————————${gl_bai}"
}
main() {
case "$1" in
up|update|upgrade)
bash <(curl -sSL gitee.com/meimolihan/script/raw/master/sh/install/check.sh)
bash /etc/profile.d/linux-check.sh
exit
;;
version|v|-v|--version)
echo -e "${gl_bufan}脚本版本: ${gl_huang}v$SH_VERSION${gl_bai}"
check_for_update
return 0
;;
help|h|-h|--help)
echo -e "${gl_bufan}可用命令:${gl_bai}"
echo -e "${gl_bufan}————————————————————————${gl_bai}"
echo -e " ${gl_huang}g${gl_bai} 显示系统信息"
echo -e " ${gl_huang}g up${gl_bai} 更新脚本到最新版"
echo -e " ${gl_huang}g version${gl_bai} 显示当前版本"
echo -e " ${gl_huang}g help${gl_bai} 显示帮助信息"
echo -e "${gl_bufan}————————————————————————${gl_bai}"
return 0
;;
esac
show_system_info
if [[ $- == *i* ]]; then
if [ -z "$LINUX_CHECK_SHOWN" ]; then
export LINUX_CHECK_SHOWN=1
fi
else
check_for_update
fi
}
main "$@"
iStoreOS 版 istoreos-check.sh
#!/bin/bash
sh_v="1.1.2"
gl_hui='\e[37m'
gl_hong='\033[31m'
gl_lv='\033[32m'
gl_huang='\033[33m'
gl_lan='\033[34m'
gl_bai='\033[0m'
gl_zi='\033[35m'
gl_bufan='\033[96m'
mobufan_sh_update() {
local sh_v_new=$(curl -s https://gitee.com/meimolihan/cmdbox/raw/master/sh/istoreos-check.sh \
| grep -o 'sh_v="[0-9.]*"' | cut -d '"' -f 2)
if [ "$sh_v" != "$sh_v_new" ]; then
echo -e "${gl_hong}发现新版本!${gl_bai}"
local current_text="${gl_bai}当前版本号 ${gl_huang}v$sh_v"
local latest_text="${gl_bai}最新版本号 ${gl_lv}v$sh_v_new"
local input_text="${gl_bai}命令行输入${gl_huang} g up"
local update_text="${gl_bai}更新至新版 ${gl_lv}v$sh_v_new"
calc_display_width() {
echo "$1" | sed 's/\x1B\[[0-9;]*[a-zA-Z]//g' | wc -m
}
local col1_width1=$(calc_display_width "$current_text")
local col2_width1=$(calc_display_width "$latest_text")
local col1_width2=$(calc_display_width "$input_text")
local col2_width2=$(calc_display_width "$update_text")
local max_col1_width=$(( col1_width1 > col1_width2 ? col1_width1 : col1_width2 ))
local max_col2_width=$(( col2_width1 > col2_width2 ? col2_width1 : col2_width2 ))
local pad_col1_1=$(( max_col1_width - col1_width1 ))
local pad_col1_2=$(( max_col1_width - col1_width2 ))
echo -e "${current_text}$(printf '%*s' $pad_col1_1) ${latest_text}${gl_bai}"
echo -e "${input_text}$(printf '%*s' $pad_col1_2) ${update_text}${gl_bai}"
echo -e "${gl_bufan}————————————————————————${gl_bai}"
fi
}
show_system_info() {
echo -e "${gl_lv}命令行输入${gl_huang}g${gl_lv}可快速启动脚本${gl_bai}"
get_local_ip() {
ifconfig br-lan | awk -F '[ :]+' '/inet addr/{print $4}'
}
get_cpu_usage() {
top -bn1 2>/dev/null | grep "Cpu(s)" | awk '{printf "%.2f%%", 100 - $8}' || echo "无法获取你想要的信息!"
}
get_uptime() {
uptime_seconds=$(awk '{print int($1)}' /proc/uptime 2>/dev/null)
if [ -n "$uptime_seconds" ]; then
hours=$((uptime_seconds / 3600))
minutes=$(( (uptime_seconds % 3600) / 60 ))
echo "${hours}时 ${minutes}分"
else
echo "无法获取你想要的信息!"
fi
}
get_default_gateway() {
local gateway
gateway=$(ip route show default 2>/dev/null | awk '/default/ {print $3}' | head -n1)
if [ -n "$gateway" ]; then
echo "$gateway"
else
echo "无法获取你想要的信息!"
fi
}
get_disk_usage() {
local disk_info
disk_info=$(df -h / 2>/dev/null | awk 'NR==2 {print $3"/"$2 " ("$5")"}')
if [ -n "$disk_info" ]; then
echo "$disk_info"
else
echo "无法获取你想要的信息!"
fi
}
clear
echo -e ""
echo -e "${gl_zi}>>> 系统信息${gl_bai}"
echo -e "${gl_bufan}————————————————————————${gl_bai}"
echo -e "${gl_bufan}主机名称 : ${gl_bai}$(cat /proc/sys/kernel/hostname 2>/dev/null || echo "Unknown")"
echo -e "${gl_bufan}内核版本 : ${gl_bai}$(uname -r)"
echo -e "${gl_bufan}————————————————————————${gl_bai}"
echo -e "${gl_bufan}CPU 架构 : ${gl_bai}$(uname -m)"
echo -e "${gl_bufan}CPU 占用 : ${gl_bai}$(get_cpu_usage)"
echo -e "${gl_bufan}————————————————————————${gl_bai}"
echo -e "${gl_bufan}IPV4内网 : ${gl_bai}$(get_local_ip)"
echo -e "${gl_bufan}默认网关 : ${gl_bai}$(get_default_gateway)"
echo -e "${gl_bufan}————————————————————————${gl_bai}"
echo -e "${gl_bufan}磁盘占用 : ${gl_bai}$(get_disk_usage)"
echo -e "${gl_bufan}运行时间 : ${gl_bai}$(get_uptime)"
echo -e "${gl_bufan}————————————————————————${gl_bai}"
}
update_script() {
echo -e "${gl_huang}正在更新脚本 ${gl_hong}.${gl_huang}.${gl_lv}.${gl_bai}"
local update_url="https://gitee.com/meimolihan/cmdbox/raw/master/sh/istoreos-check.sh"
if bash <(curl -sSL "$update_url"); then
echo -e "${gl_lv}脚本更新成功!${gl_bai}"
if alias g >/dev/null 2>&1; then
source ~/.bashrc 2>/dev/null || source ~/.bash_profile 2>/dev/null || source ~/.zshrc 2>/dev/null
fi
echo -ne "${gl_bai}即将启动新版本脚本,倒计时: ${gl_huang}2${gl_bai} 秒"
sleep 1
echo -ne "\r${gl_bai}即将启动新版本脚本,倒计时: ${gl_lv}1${gl_bai} 秒"
sleep 1
echo -e "\r${gl_lv}正在启动新版本脚本 ${gl_hong}.${gl_huang}.${gl_lv}.${gl_bai}"
if [ -f "/etc/profile.d/linux-check.sh" ]; then
bash /etc/profile.d/linux-check.sh
else
echo -e "${gl_hong}错误:找不到 /etc/profile.d/linux-check.sh 文件${gl_bai}"
return 1
fi
exit 0
else
echo -e "${gl_hong}脚本更新失败!请检查网络连接。${gl_bai}"
return 1
fi
}
main() {
if [ "$1" = "up" ] || [ "$1" = "update" ]; then
update_script
return $?
fi
if [[ $- == *i* ]]; then
if [ -z "$LINUX_CHECK_SHOWN" ]; then
show_system_info
export LINUX_CHECK_SHOWN=1
fi
else
show_system_info
fi
if [[ $- != *i* ]]; then
mobufan_sh_update
fi
}
main "$@"