refactor: 简化 shell 检测,仅支持 bash 和 zsh
- 移除 fish shell 支持 - 其他 shell 显示不支持错误并退出 - 简化 write_env_to_shell 函数 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -29,10 +29,10 @@ get_shell_rc() {
|
|||||||
else
|
else
|
||||||
echo "$HOME/.bash_profile"
|
echo "$HOME/.bash_profile"
|
||||||
fi
|
fi
|
||||||
elif [ "${SHELL##*/}" = "fish" ]; then
|
|
||||||
echo "$HOME/.config/fish/config.fish"
|
|
||||||
else
|
else
|
||||||
echo "$HOME/.profile"
|
print_error "不支持当前 shell: ${SHELL##*/}"
|
||||||
|
print_error "仅支持 bash 和 zsh"
|
||||||
|
exit 1
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -53,18 +53,13 @@ write_env_to_shell() {
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
# 转义特殊字符(使用单引号包裹更安全)
|
# 转义特殊字符(使用单引号包裹更安全)
|
||||||
local export_line
|
local export_line="export $var_name='$var_value'"
|
||||||
if [ "${SHELL##*/}" = "fish" ]; then
|
|
||||||
export_line="set -Ux $var_name '$var_value'"
|
|
||||||
else
|
|
||||||
export_line="export $var_name='$var_value'"
|
|
||||||
fi
|
|
||||||
|
|
||||||
# 删除旧的同名变量行,添加新行
|
# 删除旧的同名变量行,添加新行
|
||||||
local tmp_file
|
local tmp_file
|
||||||
tmp_file=$(mktemp)
|
tmp_file=$(mktemp)
|
||||||
if [ -s "$rc_file" ]; then
|
if [ -s "$rc_file" ]; then
|
||||||
grep -v "^export $var_name=" "$rc_file" | grep -v "^set -Ux $var_name " >"$tmp_file" 2>/dev/null || true
|
grep -v "^export $var_name=" "$rc_file" >"$tmp_file" 2>/dev/null || true
|
||||||
fi
|
fi
|
||||||
echo "$export_line" >>"$tmp_file"
|
echo "$export_line" >>"$tmp_file"
|
||||||
|
|
||||||
|
|||||||
@@ -38,10 +38,10 @@ get_shell_rc() {
|
|||||||
else
|
else
|
||||||
echo "$HOME/.bash_profile"
|
echo "$HOME/.bash_profile"
|
||||||
fi
|
fi
|
||||||
elif [ "${SHELL##*/}" = "fish" ]; then
|
|
||||||
echo "$HOME/.config/fish/config.fish"
|
|
||||||
else
|
else
|
||||||
echo "$HOME/.profile"
|
print_error "不支持当前 shell: ${SHELL##*/}"
|
||||||
|
print_error "仅支持 bash 和 zsh"
|
||||||
|
exit 1
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -62,18 +62,13 @@ write_env_to_shell() {
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
# 转义特殊字符(使用单引号包裹更安全)
|
# 转义特殊字符(使用单引号包裹更安全)
|
||||||
local export_line
|
local export_line="export $var_name='$var_value'"
|
||||||
if [ "${SHELL##*/}" = "fish" ]; then
|
|
||||||
export_line="set -Ux $var_name '$var_value'"
|
|
||||||
else
|
|
||||||
export_line="export $var_name='$var_value'"
|
|
||||||
fi
|
|
||||||
|
|
||||||
# 删除旧的同名变量行,添加新行
|
# 删除旧的同名变量行,添加新行
|
||||||
local tmp_file
|
local tmp_file
|
||||||
tmp_file=$(mktemp)
|
tmp_file=$(mktemp)
|
||||||
if [ -s "$rc_file" ]; then
|
if [ -s "$rc_file" ]; then
|
||||||
grep -v "^export $var_name=" "$rc_file" | grep -v "^set -Ux $var_name " >"$tmp_file" 2>/dev/null || true
|
grep -v "^export $var_name=" "$rc_file" >"$tmp_file" 2>/dev/null || true
|
||||||
fi
|
fi
|
||||||
echo "$export_line" >>"$tmp_file"
|
echo "$export_line" >>"$tmp_file"
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user