## 问题描述 在使用 `set -u` 严格模式时,`source <(curl ...)` 或 `source <(wget ...)` 的 process substitution 方式会在脚本退出时产生错误: ``` /dev/fd/63: line 1: fifo: unbound variable ``` ## 根本原因 Process substitution 创建的临时文件描述符(如 /dev/fd/63)在退出时 与 Bash 的 `set -u` 严格模式存在兼容性问题,导致错误消息。 ## 修复方案 将 process substitution 替换为临时文件方案: **旧方案(有问题):** ```bash source <(curl -fsSL "$url") ``` **新方案(兼容性好):** ```bash temp_loader=$(mktemp) curl -fsSL "$url" -o "$temp_loader" source "$temp_loader" rm -f "$temp_loader" ``` ## 修改的文件 批量修复了所有 7 个脚本的远程加载逻辑: - oci/create_instance.sh - linux/create_raid0_array.sh - linux/install_oh_my_zsh.sh - linux/repartition_disks.sh - gcp/create_ai_projects.sh - gcp/delete_all_projects.sh - common/demo_usage.sh ## 优势 - ✅ 避免 process substitution 的兼容性问题 - ✅ 与 `set -u` 严格模式完全兼容 - ✅ 显式的临时文件管理,更易理解 - ✅ 确保所有分支都正确清理临时文件 - ✅ 保持 curl/wget 双重支持不变
176 lines
5.5 KiB
Bash
176 lines
5.5 KiB
Bash
#!/bin/bash
|
||
# ============================================================================
|
||
# 文件名: install_oh_my_zsh.sh
|
||
# 描述: 安装和配置 Oh My Zsh 及常用插件
|
||
# 作者: Cloud Tools Project
|
||
# 版本: 2.1.0(支持远程库加载)
|
||
# ============================================================================
|
||
|
||
set -euo pipefail
|
||
|
||
# ============================================================================
|
||
# 远程库加载配置
|
||
# ============================================================================
|
||
|
||
# 远程仓库 URL(可通过环境变量覆盖)
|
||
readonly REMOTE_BASE_URL="${REMOTE_LIB_URL:-https://gitea.bcde.io/wangdefa/tools/raw/branch/main}"
|
||
|
||
# 获取脚本目录(用于本地加载)
|
||
readonly SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||
readonly PROJECT_ROOT="$(dirname "$SCRIPT_DIR")"
|
||
|
||
#
|
||
# 智能加载公共库
|
||
#
|
||
# 加载策略:
|
||
# 1. 如果 FORCE_REMOTE=1,强制使用远程库
|
||
# 2. 否则尝试使用本地库
|
||
# 3. 本地库不存在时自动回退到远程库
|
||
#
|
||
load_common_libs() {
|
||
local use_remote=false
|
||
|
||
# 检查是否强制远程
|
||
if [[ "${FORCE_REMOTE:-0}" == "1" ]]; then
|
||
echo "[INFO] 强制使用远程库 (FORCE_REMOTE=1)" >&2
|
||
use_remote=true
|
||
# 检查本地库是否存在
|
||
elif [[ -f "${PROJECT_ROOT}/common/logging.sh" ]] && [[ -f "${PROJECT_ROOT}/common/error_handler.sh" ]]; then
|
||
# shellcheck disable=SC1091
|
||
source "${PROJECT_ROOT}/common/logging.sh"
|
||
# shellcheck disable=SC1091
|
||
source "${PROJECT_ROOT}/common/error_handler.sh"
|
||
return 0
|
||
else
|
||
echo "[WARN] 本地库不存在,使用远程库" >&2
|
||
use_remote=true
|
||
fi
|
||
|
||
# 使用远程库
|
||
if [[ "$use_remote" == "true" ]]; then
|
||
# 下载到临时文件(避免 process substitution 与 set -u 的交互问题)
|
||
local temp_loader
|
||
temp_loader=$(mktemp)
|
||
|
||
if command -v curl &>/dev/null; then
|
||
echo "[INFO] 使用 curl 下载远程库..." >&2
|
||
if curl -fsSL "${REMOTE_BASE_URL}/common/remote_loader.sh" -o "$temp_loader" 2>/dev/null; then
|
||
# shellcheck disable=SC1090
|
||
if source "$temp_loader"; then
|
||
rm -f "$temp_loader"
|
||
return 0
|
||
fi
|
||
fi
|
||
elif command -v wget &>/dev/null; then
|
||
echo "[INFO] 使用 wget 下载远程库..." >&2
|
||
if wget -qO "$temp_loader" "${REMOTE_BASE_URL}/common/remote_loader.sh" 2>/dev/null; then
|
||
# shellcheck disable=SC1090
|
||
if source "$temp_loader"; then
|
||
rm -f "$temp_loader"
|
||
return 0
|
||
fi
|
||
fi
|
||
fi
|
||
|
||
rm -f "$temp_loader"
|
||
echo "[ERROR] 无法加载公共库" >&2
|
||
echo "[ERROR] - 本地库不存在" >&2
|
||
echo "[ERROR] - 远程下载失败(需要 curl 或 wget)" >&2
|
||
echo "[ERROR] - 仓库 URL: ${REMOTE_BASE_URL}" >&2
|
||
exit 1
|
||
fi
|
||
}
|
||
|
||
# 加载公共库
|
||
load_common_libs
|
||
|
||
# 配置路径
|
||
readonly INSTALL_DIR="${HOME}/.oh-my-zsh"
|
||
readonly ZSH_CONFIG="${HOME}/.zshrc"
|
||
readonly ZSH_CUSTOM="${INSTALL_DIR}/custom"
|
||
|
||
#
|
||
# 检查并安装缺失的软件包
|
||
#
|
||
# 参数:
|
||
# $@ - 要检查的软件包列表
|
||
#
|
||
check_and_install_packages() {
|
||
local packages=("$@")
|
||
local missing_packages=()
|
||
|
||
for package in "${packages[@]}"; do
|
||
if ! check_command "$package"; then
|
||
missing_packages+=("$package")
|
||
fi
|
||
done
|
||
|
||
if [[ ${#missing_packages[@]} -gt 0 ]]; then
|
||
log_warning "缺少以下软件包: ${missing_packages[*]}"
|
||
if command -v apt-get &>/dev/null; then
|
||
run_command "安装软件包" apt-get update
|
||
run_command "安装软件包" apt-get install -y "${missing_packages[@]}"
|
||
else
|
||
log_error "只支持 apt-get 包管理器"
|
||
exit 1
|
||
fi
|
||
fi
|
||
log_success "所有软件包已安装"
|
||
}
|
||
|
||
#
|
||
# 安装 Oh My Zsh
|
||
#
|
||
install_zsh() {
|
||
log_info "安装 zsh..."
|
||
run_command "更新软件包" sudo apt update
|
||
run_command "安装 zsh" sudo apt install zsh -y
|
||
sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
|
||
}
|
||
|
||
#
|
||
# 主函数
|
||
#
|
||
main() {
|
||
log_info "============ Oh My Zsh 安装程序 ============"
|
||
log_info "版本: 2.1.0 (支持远程库加载)"
|
||
|
||
check_and_install_packages "sudo" "git" "curl" "zsh"
|
||
|
||
if [ ! -d "$INSTALL_DIR" ]; then
|
||
install_zsh
|
||
else
|
||
log_success "oh-my-zsh 已安装"
|
||
fi
|
||
|
||
# 安装插件
|
||
if [ ! -d "$ZSH_CUSTOM/plugins/zsh-autosuggestions" ]; then
|
||
log_info "安装 zsh-autosuggestions..."
|
||
run_command "克隆插件" git clone https://github.com/zsh-users/zsh-autosuggestions "$ZSH_CUSTOM/plugins/zsh-autosuggestions"
|
||
fi
|
||
|
||
if [ ! -d "$ZSH_CUSTOM/plugins/zsh-syntax-highlighting" ]; then
|
||
log_info "安装 zsh-syntax-highlighting..."
|
||
run_command "克隆插件" git clone https://github.com/zsh-users/zsh-syntax-highlighting.git "$ZSH_CUSTOM/plugins/zsh-syntax-highlighting"
|
||
fi
|
||
|
||
log_success "安装完成"
|
||
|
||
# 配置 .zshrc
|
||
log_info "修改 .zshrc 文件..."
|
||
cp "$ZSH_CONFIG" "$ZSH_CONFIG.bak"
|
||
sed -i 's/plugins=(git)/plugins=(git zsh-autosuggestions zsh-syntax-highlighting extract)/' "$ZSH_CONFIG"
|
||
sed -i 's/ZSH_THEME="robbyrussell"/ZSH_THEME="random"/' "$ZSH_CONFIG"
|
||
log_success "配置完成"
|
||
|
||
# 设置默认 shell
|
||
log_info "设置 zsh 为默认 shell..."
|
||
chsh -s /bin/zsh
|
||
log_success "设置完成"
|
||
|
||
log_info "重启 zsh..."
|
||
zsh
|
||
}
|
||
|
||
# 执行主函数
|
||
main "$@" |