From 39bd19e9dfb9510f47bebd03176af35f8dc357e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E6=80=A7=E9=A9=8A?= Date: Thu, 5 Nov 2026 08:02:57 +0800 Subject: [PATCH] install.sh: an update needs no sudo when the system packages are already there Fresh systems still fetch the package lists first. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Uirha3oEtWeHh58zA7ErHa --- install.sh | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/install.sh b/install.sh index 4d112b0..16e8923 100755 --- a/install.sh +++ b/install.sh @@ -95,7 +95,6 @@ linux_packages() { # $1: build | app | hands | server case $PM in apt-get) - $SUDO apt-get update -qq || warn "apt-get update 有錯誤,繼續試試看" case $1 in server) set -- curl ca-certificates git ;; build) set -- build-essential pkg-config git curl ca-certificates libasound2-dev ;; @@ -106,8 +105,19 @@ linux_packages() { libxkbcommon-x11-0 "libgl1|libgl1-mesa-glx" xdg-utils libnotify-bin libsecret-tools espeak-ng zenity \ xclip wl-clipboard ffmpeg fonts-noto-cjk ;; esac + # A fresh system has no package lists yet: fetch them first, or nothing can be picked. + if ! ls /var/lib/apt/lists/*_Packages >/dev/null 2>&1; then + $SUDO apt-get update -qq || warn "apt-get update 有錯誤,繼續試試看" + UPDATED=1 + fi pkgs=$(apt_pick "$@" | tr '\n' ' ') - info "apt-get install $pkgs" + # Already there (an update): nothing to install, no sudo password needed. + missing="" + for p in $pkgs; do dpkg -s "$p" >/dev/null 2>&1 || missing="$missing $p"; done + if [ -z "$missing" ]; then info "都已經裝好了"; return 0; fi + pkgs=$missing + [ -n "${UPDATED:-}" ] || $SUDO apt-get update -qq || warn "apt-get update 有錯誤,繼續試試看" + info "apt-get install$pkgs" $SUDO env DEBIAN_FRONTEND=noninteractive apt-get install -y -qq $pkgs >/dev/null || die "安裝系統套件失敗。" "請看上面的錯誤,或手動執行:sudo apt-get install $pkgs" ;; dnf)