当前 src/cli/menus/install.ts 第 49 行使用管道执行:
curl -fsSL https://claude.ai/install.sh | bash这在 Ubuntu/Debian 上会失败,因为 /bin/sh 链接到 dash,不支持 pipefail。
修改前:
{
name: '官方脚本安装',
command: 'curl -fsSL https://claude.ai/install.sh | bash',
description: '需要代理或外网访问',
platforms: ['darwin', 'linux'],
recommended: true,
},修改后:
{
name: '官方脚本安装',
command: 'temp=$(mktemp) && trap "rm -f $temp" EXIT && curl -fsSL https://claude.ai/install.sh -o "$temp" && bash "$temp"',
description: '需要代理或外网访问',
platforms: ['darwin', 'linux'],
recommended: true,
},修改前:
if (proxyUrl) {
// 如果是 curl 命令,直接在命令中添加代理参数
if (finalCommand.includes('curl')) {
finalCommand = finalCommand.replace('curl ', `curl --proxy ${proxyUrl} `);
} else {
// 其他命令使用环境变量
envVars = {
http_proxy: proxyUrl,
https_proxy: proxyUrl,
HTTP_PROXY: proxyUrl,
HTTPS_PROXY: proxyUrl,
};
}
showInfo(`使用代理: ${proxyUrl}`);
}修改后:
if (proxyUrl) {
// 如果是 curl 命令,直接在命令中添加代理参数
if (finalCommand.includes('curl')) {
// 在所有 curl 后面添加代理参数(支持多个 curl)
finalCommand = finalCommand.replace(/curl\s+/g, `curl --proxy ${proxyUrl} `);
} else {
// 其他命令使用环境变量
envVars = {
http_proxy: proxyUrl,
https_proxy: proxyUrl,
HTTP_PROXY: proxyUrl,
HTTPS_PROXY: proxyUrl,
};
}
showInfo(`使用代理: ${proxyUrl}`);
}# 1. 创建临时文件
temp=$(mktemp)
# 2. 设置自动清理(无论如何退出都会清理)
trap "rm -f $temp" EXIT
# 3. 下载安装脚本
curl -fsSL https://claude.ai/install.sh -o "$temp"
# 4. 执行脚本
bash "$temp"
# 5. trap 自动清理临时文件- 避免管道执行 - 先下载再执行,避免 pipefail 问题
- 自动清理 - 使用 trap 确保临时文件总是被清理
- 错误传播 - 使用
&&连接,任何步骤失败都会停止 - 兼容性 - 适用于所有 POSIX 兼容的 shell(dash/bash/zsh)
使用正则表达式 /curl\s+/g 替换所有 curl 命令,确保:
- 原始命令:
curl -fsSL - 带代理的命令:
curl --proxy http://127.0.0.1:7890 -fsSL - 临时文件中的 curl:也会被替换
# 测试修复后的命令
temp=$(mktemp) && trap "rm -f $temp" EXIT && curl -fsSL https://claude.ai/install.sh -o "$temp" && bash "$temp"
# 应该成功执行,不会报 pipefail 错误# 同样应该正常工作
temp=$(mktemp) && trap "rm -f $temp" EXIT && curl -fsSL https://claude.ai/install.sh -o "$temp" && bash "$temp"你同事提到的修复:
temp=$(mktemp)
curl --proxy $proxy -fsSL https://claude.ai/install.sh -o "$temp"
bash "$temp"
rm -f "$temp"存在的问题:
- ❌ curl 失败时,bash 仍会执行(可能执行空文件)
- ❌ mktemp 失败未处理
- ❌ 退出码丢失(无法判断安装是否成功)
- ❌ 中途失败时,临时文件可能残留
我们的方案优势:
- ✅ 使用
&&连接,任何步骤失败都会停止 - ✅ 使用
trap自动清理,即使失败也会清理 - ✅ 退出码正确传播
- ✅ 简洁明了,易于维护
- 修改
src/cli/menus/install.ts第 49 行 - 修改第 394 行的正则替换
- 运行
npm run build构建 - 在 Ubuntu/Debian 上测试安装功能
- 在 macOS 上测试确保不受影响