fix: 修复所有脚本的 process substitution 兼容性问题
## 问题描述 在使用 `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 双重支持不变
This commit is contained in:
@@ -48,20 +48,31 @@ load_common_libs() {
|
||||
|
||||
# 使用远程库
|
||||
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
|
||||
# shellcheck disable=SC1090
|
||||
if source <(curl -fsSL "${REMOTE_BASE_URL}/common/remote_loader.sh" 2>/dev/null); then
|
||||
return 0
|
||||
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
|
||||
# shellcheck disable=SC1090
|
||||
if source <(wget -qO- "${REMOTE_BASE_URL}/common/remote_loader.sh" 2>/dev/null); then
|
||||
return 0
|
||||
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
|
||||
|
||||
@@ -48,20 +48,31 @@ load_common_libs() {
|
||||
|
||||
# 使用远程库
|
||||
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
|
||||
# shellcheck disable=SC1090
|
||||
if source <(curl -fsSL "${REMOTE_BASE_URL}/common/remote_loader.sh" 2>/dev/null); then
|
||||
return 0
|
||||
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
|
||||
# shellcheck disable=SC1090
|
||||
if source <(wget -qO- "${REMOTE_BASE_URL}/common/remote_loader.sh" 2>/dev/null); then
|
||||
return 0
|
||||
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
|
||||
|
||||
@@ -49,20 +49,31 @@ load_common_libs() {
|
||||
|
||||
# 使用远程库
|
||||
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
|
||||
# shellcheck disable=SC1090
|
||||
if source <(curl -fsSL "${REMOTE_BASE_URL}/common/remote_loader.sh" 2>/dev/null); then
|
||||
return 0
|
||||
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
|
||||
# shellcheck disable=SC1090
|
||||
if source <(wget -qO- "${REMOTE_BASE_URL}/common/remote_loader.sh" 2>/dev/null); then
|
||||
return 0
|
||||
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
|
||||
|
||||
@@ -49,20 +49,31 @@ load_common_libs() {
|
||||
|
||||
# 使用远程库
|
||||
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
|
||||
# shellcheck disable=SC1090
|
||||
if source <(curl -fsSL "${REMOTE_BASE_URL}/common/remote_loader.sh" 2>/dev/null); then
|
||||
return 0
|
||||
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
|
||||
# shellcheck disable=SC1090
|
||||
if source <(wget -qO- "${REMOTE_BASE_URL}/common/remote_loader.sh" 2>/dev/null); then
|
||||
return 0
|
||||
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
|
||||
|
||||
@@ -48,20 +48,31 @@ load_common_libs() {
|
||||
|
||||
# 使用远程库
|
||||
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
|
||||
# shellcheck disable=SC1090
|
||||
if source <(curl -fsSL "${REMOTE_BASE_URL}/common/remote_loader.sh" 2>/dev/null); then
|
||||
return 0
|
||||
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
|
||||
# shellcheck disable=SC1090
|
||||
if source <(wget -qO- "${REMOTE_BASE_URL}/common/remote_loader.sh" 2>/dev/null); then
|
||||
return 0
|
||||
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
|
||||
|
||||
@@ -49,20 +49,31 @@ load_common_libs() {
|
||||
|
||||
# 使用远程库
|
||||
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
|
||||
# shellcheck disable=SC1090
|
||||
if source <(curl -fsSL "${REMOTE_BASE_URL}/common/remote_loader.sh" 2>/dev/null); then
|
||||
return 0
|
||||
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
|
||||
# shellcheck disable=SC1090
|
||||
if source <(wget -qO- "${REMOTE_BASE_URL}/common/remote_loader.sh" 2>/dev/null); then
|
||||
return 0
|
||||
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
|
||||
|
||||
@@ -48,20 +48,31 @@ load_common_libs() {
|
||||
|
||||
# 使用远程库
|
||||
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
|
||||
# shellcheck disable=SC1090
|
||||
if source <(curl -fsSL "${REMOTE_BASE_URL}/common/remote_loader.sh" 2>/dev/null); then
|
||||
return 0
|
||||
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
|
||||
# shellcheck disable=SC1090
|
||||
if source <(wget -qO- "${REMOTE_BASE_URL}/common/remote_loader.sh" 2>/dev/null); then
|
||||
return 0
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user