#!/bin/bash # 在设备上装 git,准备往 Gitee 推代码。 set -u echo "===== 1. 环境 =====" df -h / | tail -1 | sed 's/^/ /' echo " apt: $(command -v apt-get >/dev/null 2>&1 && echo 有 || echo 没有)" echo echo "===== 2. apt 源能不能用 =====" echo -n " 到 deb.debian.org: " timeout 10 bash -c 'exec 3<>/dev/tcp/deb.debian.org/80' 2>/dev/null \ && echo "通" || echo "不通" echo -n " 到 mirrors.aliyun.com: " timeout 10 bash -c 'exec 3<>/dev/tcp/mirrors.aliyun.com/80' 2>/dev/null \ && echo "通" || echo "不通" echo echo "===== 3. 装 git =====" export DEBIAN_FRONTEND=noninteractive # 先换成国内源(默认源在国内 4G 下常常超时) if ! grep -q 'mirrors.aliyun.com' /etc/apt/sources.list 2>/dev/null; then echo " 换成阿里云源" cp /etc/apt/sources.list /etc/apt/sources.list.bak 2>/dev/null || true cat > /etc/apt/sources.list <<'EOF' deb https://mirrors.aliyun.com/debian bookworm main contrib non-free deb https://mirrors.aliyun.com/debian bookworm-updates main contrib non-free deb https://mirrors.aliyun.com/debian-security bookworm-security main contrib non-free EOF fi timeout 240 apt-get update -qq 2>&1 | tail -3 | sed 's/^/ /' echo " 开始装…" timeout 300 apt-get install -y -qq git 2>&1 | tail -5 | sed 's/^/ /' echo echo "===== 4. 结果 =====" if command -v git >/dev/null 2>&1; then echo " ✓ git $(git --version | awk '{print $3}')" else echo " ✗ 还是没装上" fi