#!/bin/bash # 把 llm.env.snippet 里的 KEY=VALUE 写入生产 server/.env(不覆盖其它项) # 用法: bash prod-upsert-llm-env.sh /path/to/llm.env.snippet set -euo pipefail ENV_FILE="${ENV_FILE:-/opt/ai-fresh-delivery/server/.env}" SNIPPET="${1:-}" if [ -z "$SNIPPET" ] || [ ! -f "$SNIPPET" ]; then echo "Missing snippet file" exit 1 fi if [ ! -f "$ENV_FILE" ]; then echo "Missing $ENV_FILE" exit 1 fi tmp="$(mktemp)" cp "$ENV_FILE" "$tmp" while IFS= read -r line || [ -n "$line" ]; do line="${line%%$'\r'}" [[ -z "${line// /}" || "$line" =~ ^[[:space:]]*# ]] && continue [[ "$line" =~ ^[A-Za-z_][A-Za-z0-9_]*= ]] || continue key="${line%%=*}" val="${line#*=}" [ -z "$val" ] && continue grep -v "^${key}=" "$tmp" > "${tmp}.next" || true mv "${tmp}.next" "$tmp" printf '%s=%s\n' "$key" "$val" >> "$tmp" echo "upsert $key" done < "$SNIPPET" cp "$tmp" "$ENV_FILE" rm -f "$tmp" echo "Updated $ENV_FILE"