Files
scripts-public/linux-managements/menu.sh
T

101 lines
4.5 KiB
Bash

#!/bin/bash
set -euo pipefail
RED='\033[0;31m'; GREEN='\033[0;32m'; YELLOW='\033[1;33m'
BLUE='\033[0;34m'; CYAN='\033[0;36m'; BOLD='\033[1m'; NC='\033[0m'
_NAMES=(); _DESCS=(); _URLS=()
add() { _NAMES+=("$1"); _DESCS+=("$2"); _URLS+=("$3"); }
# Read from terminal even when script is run via `curl ... | bash`.
prompt_read() {
local __var_name="$1"
local __prompt="$2"
local __input=""
if [ -r /dev/tty ]; then
read -r -p "$__prompt" __input < /dev/tty
else
read -r -p "$__prompt" __input
fi
printf -v "$__var_name" '%s' "$__input"
}
# ════════════════════════════════════════════════════════════════════════════════
# CONFIGURATION — 在此处添加或修改脚本条目
# 用法: add "<显示名称>" "<脚本说明>" "<脚本 URL>"
# ════════════════════════════════════════════════════════════════════════════════
add "Linux 初始化配置" \
"配置 HTTP 代理、SSH 密钥、安装 Homebrew 及常用软件、配置 Fish Shell、同步 Dotfiles" \
"https://git.mitsea.com/FlintyLemming/scripts-public/raw/branch/main/linux-managements/setup.sh"
add "安装 Docker Engine(中国大陆服务器)" \
"自动检测发行版,经由南洋理工镜像源安装 Docker Engine、Compose、Buildx 等组件" \
"https://git.mitsea.com/FlintyLemming/scripts-public/raw/branch/main/linux-managements/install-docker.sh"
add "配置 Snapper 快照" \
"为 Btrfs 子卷配置 Snapper 自动快照,按天/周/月/年策略保留快照并启用 systemd 定时器" \
"https://git.mitsea.com/FlintyLemming/scripts-public/raw/branch/main/linux-managements/setup-snapper.sh"
# ════════════════════════════════════════════════════════════════════════════════
# 以下为脚本逻辑,无需修改
# ════════════════════════════════════════════════════════════════════════════════
draw_menu() {
echo -e "\n${BOLD}${BLUE}╔══════════════════════════════════════════════════════════════╗${NC}"
echo -e "${BOLD}${BLUE}║ Linux Management Scripts ║${NC}"
echo -e "${BOLD}${BLUE}╚══════════════════════════════════════════════════════════════╝${NC}\n"
local total="${#_NAMES[@]}"
for (( i=0; i<total; i++ )); do
echo -e " ${BOLD}${CYAN}[$((i+1))]${NC} ${BOLD}${_NAMES[$i]}${NC}"
echo -e " ${YELLOW}${_DESCS[$i]}${NC}"
echo
done
echo -e " ${BOLD}${RED}[0]${NC} 退出\n"
}
run_script() {
local idx=$1
local name="${_NAMES[$idx]}"
local url="${_URLS[$idx]}"
echo -e "\n${GREEN}[OK]${NC} 正在执行: ${BOLD}$name${NC}"
echo -e "${CYAN}[INFO]${NC} 来源: $url\n"
echo -e "${BLUE}──────────────────────────────────────────────────────────────${NC}\n"
if command -v curl &>/dev/null; then
bash <(curl -fsSL "$url")
elif command -v wget &>/dev/null; then
bash <(wget -qO- "$url")
else
echo -e "${RED}[ERR]${NC} 未找到 curl 或 wget,无法下载脚本。" >&2
exit 1
fi
}
main() {
local total="${#_NAMES[@]}"
while true; do
draw_menu
prompt_read choice "请选择要执行的脚本 [0-$total]: "
if [[ "$choice" == "0" ]]; then
echo -e "\n${CYAN}[INFO]${NC} 已退出。\n"
exit 0
fi
if ! [[ "$choice" =~ ^[0-9]+$ ]] || (( choice < 1 || choice > total )); then
echo -e "\n${RED}[ERR]${NC} 无效选项,请输入 0 到 $total 之间的数字。\n"
continue
fi
run_script $(( choice - 1 ))
break
done
}
main