mac 系统下实现 claude code 不同的中转站自由切换(改改就适合 windows 了)_claude code 中转
脚本概述
这是一个用于管理 Claude Code 多个配置文件的 Bash 脚本工具,可以在不同的 Claude 配置之间快速切换。脚本位于 ~/.claude/switch.sh
,主要用于在多个 settings.json
配置文件之间进行切换。
主要功能
1. 配置切换功能
- any 配置: 切换到
settings-any.json
- yes 配置: 切换到
settings-yes.json
- pack 配置: 切换到
settings-pack.json
2. 配置管理功能
- 显示当前激活的配置内容
- 自动备份当前配置文件(
settings.json.bak
) - 错误检查和异常处理
3. 使用模式
- 交互模式: 直接运行脚本,通过菜单选择配置
- 命令行模式: 直接传参执行,如
./switch.sh any
脚本结构
核心函数
show_menu()
: 显示配置选择菜单show_current()
: 显示当前配置文件内容switch_config()
: 执行配置切换逻辑main()
: 主控制逻辑,处理交互和参数
配置目录
- 默认配置目录:
$HOME/.claude
- 主配置文件:
settings.json
- 备用配置文件:
settings-{name}.json
使用方法
交互模式
# 进入交互菜单./switch.sh# 或者bash ~/.claude/switch.sh
命令行模式
# 直接切换到指定配置./switch.sh any # 切换到 any 配置./switch.sh yes # 切换到 yes 配置./switch.sh pack # 切换到 pack 配置./switch.sh 4 # 显示当前配置
全局环境变量配置
1. 添加到 PATH
将脚本目录添加到系统 PATH 中,以便全局访问:
# 编辑 shell 配置文件vim ~/.bashrc # 或 ~/.zshrc (macOS 默认)# 添加以下内容export PATH=\"$HOME/.claude:$PATH\"# 重新加载配置source ~/.bashrc # 或 source ~/.zshrc
2. 创建别名
在 shell 配置文件中添加别名:
# 添加到 ~/.bashrc 或 ~/.zshrcalias claude-switch=\'bash ~/.claude/switch.sh\'alias cs=\'bash ~/.claude/switch.sh\'# 使配置生效source ~/.bashrc # 或 source ~/.zshrc
3. 设置脚本可执行权限
chmod +x ~/.claude/switch.sh
重要注意事项
⚠️ 使用前检查
-
确保配置文件存在:
~/.claude/settings-any.json
~/.claude/settings-yes.json
~/.claude/settings-pack.json
-
验证配置目录权限: 确保对
~/.claude
目录有完整的读写权限
⚠️ 安全注意事项
- 脚本会自动备份当前配置为
settings.json.bak
- 切换失败时不会损坏原有配置
- 建议定期备份重要配置文件
⚠️ 错误处理
脚本包含完善的错误检查:
- 配置目录不存在检查
- 源配置文件存在性验证
- 文件复制操作成功性验证
配置文件示例结构
每个 settings-{name}.json
文件应该是有效的 Claude Code 配置文件,例如:
{ \"apiKey\": \"your-api-key\", \"model\": \"claude-3-sonnet-20240229\", \"maxTokens\": 4096, \"temperature\": 0.7}
故障排除
常见问题
- 权限错误: 使用
chmod +x ~/.claude/switch.sh
设置执行权限 - 配置文件不存在: 检查
~/.claude/
目录下是否有对应的配置文件 - 路径问题: 确认脚本中的
CONFIG_DIR
设置正确
调试方法
# 检查配置目录ls -la ~/.claude/# 测试脚本语法bash -n ~/.claude/switch.sh# 显示详细执行过程bash -x ~/.claude/switch.sh
这个工具可以帮助您在不同的 Claude 配置环境之间快速切换,提高工作效率。
脚本全部内容:
#!/bin/bash# 配置目录设置CONFIG_DIR=\"$HOME/.claude\"# 显示菜单函数show_menu() { echo echo \"======================================\" echo \" 配置切换工具\" echo \"=====================================\" echo echo \"请选择要切换的配置:\" echo echo \"[1] any - 切换到 settings-any.json\" echo \"[2] yes - 切换到 settings-yes.json\" echo \"[3] pack - 切换到 settings-pack.json\" echo \"[4] 显示当前配置\" echo \"[0] 退出\" echo}# 显示当前配置函数show_current() { echo echo \"当前配置文件内容:\" echo \"======================================\" if [[ -f \"$CONFIG_DIR/settings.json\" ]]; then cat \"$CONFIG_DIR/settings.json\" else echo \"settings.json 文件不存在\" fi echo echo \"======================================\" echo}# 切换配置函数switch_config() { local target=\"$1\" # 显示当前配置目录 echo \"配置目录: $CONFIG_DIR\" echo # 检查配置目录是否存在 if [[ ! -d \"$CONFIG_DIR\" ]]; then echo \"错误: 配置目录不存在 \\\"$CONFIG_DIR\\\"\" echo \"请检查脚本中的 CONFIG_DIR 设置\" echo return 1 fi # 检查源文件是否存在 if [[ ! -f \"$CONFIG_DIR/settings-$target.json\" ]]; then echo echo \"错误: 找不到配置文件 \\\"settings-$target.json\\\"\" echo \"请确保文件存在于目录: $CONFIG_DIR\" echo return 1 fi # 备份当前配置 if [[ -f \"$CONFIG_DIR/settings.json\" ]]; then cp \"$CONFIG_DIR/settings.json\" \"$CONFIG_DIR/settings.json.bak\" 2>/dev/null fi # 复制新配置 if cp \"$CONFIG_DIR/settings-$target.json\" \"$CONFIG_DIR/settings.json\" 2>/dev/null; then echo echo \"成功: 已切换到 \\\"$target\\\" 配置\" echo return 0 else echo echo \"错误: 切换配置失败\" echo return 1 fi}# 主逻辑main() { # 检查是否提供了参数 if [[ -n \"$1\" ]]; then # 直接参数模式 choice=\"$1\" else # 交互模式 while true; do show_menu read -p \"请输入选项 (1-4, 0 退出): \" choice case \"$choice\" in 1|any) if switch_config \"any\"; then if [[ -z \"$1\" ]]; then echo \"按任意键继续...\" read -n 1 continue fi else if [[ -z \"$1\" ]]; then echo \"按任意键继续...\" read -n 1 continue fi fi break ;; 2|yes) if switch_config \"yes\"; then if [[ -z \"$1\" ]]; then echo \"按任意键继续...\" read -n 1 continue fi else if [[ -z \"$1\" ]]; then echo \"按任意键继续...\" read -n 1 continue fi fi break ;; 3|pack) if switch_config \"pack\"; then if [[ -z \"$1\" ]]; then echo \"按任意键继续...\" read -n 1 continue fi else if [[ -z \"$1\" ]]; then echo \"按任意键继续...\" read -n 1 continue fi fi break ;; 4) show_current if [[ -z \"$1\" ]]; then echo \"Press any key to continue...\" read -n 1 continue fi break ;; 0) exit 0 ;; *) echo echo \"错误: 无效选项 \\\"$choice\\\"\" echo if [[ -z \"$1\" ]]; then echo \"Press any key to continue...\" read -n 1 continue else exit 1 fi ;; esac done return fi # 处理直接参数 case \"$choice\" in 1|any) switch_config \"any\" ;; 2|yes) switch_config \"yes\" ;; 3|pack) switch_config \"pack\" ;; 4) show_current ;; 0) exit 0 ;; *) echo echo \"Error: Invalid option \\\"$choice\\\"\" echo exit 1 ;; esac}# 运行主函数main \"$@\"
Windows 系统下 bat 的全部内容:
@echo offsetlocal enabledelayedexpansionrem Config Directory Settingset \"CONFIG_DIR=C:\\Users\\muxiongxiong\\.claude\"rem Check if parameter providedif \"%1\"==\"\" goto interactiverem Direct parameter modeset \"choice=%1\"goto process_choice:interactiveecho.echo ======================================echo Config Switch Toolecho ======================================echo.echo Please select config to switch:echo.echo [1] any - Switch to settings-any.jsonecho [2] yes - Switch to settings-yes.json echo [3] pack - Switch to settings-pack.jsonecho [4] Show current configecho [0] Exitecho.set /p \"choice=Enter option (1-4, 0 to exit): \":process_choicerem Process choiceif \"%choice%\"==\"1\" set \"target=any\"if \"%choice%\"==\"2\" set \"target=yes\"if \"%choice%\"==\"3\" set \"target=pack\"if \"%choice%\"==\"4\" goto show_currentif \"%choice%\"==\"0\" goto exit_scriptif \"%choice%\"==\"any\" set \"target=any\"if \"%choice%\"==\"yes\" set \"target=yes\"if \"%choice%\"==\"pack\" set \"target=pack\"rem Check if target is setif not defined target ( echo. echo Error: Invalid option \"%choice%\" echo. if \"%1\"==\"\" ( pause goto interactive ) else ( goto exit_script ))rem Execute config switchgoto switch_config:switch_configrem Show current config directoryecho Config directory: %CONFIG_DIR%echo.rem Check if config directory existsif not exist \"%CONFIG_DIR%\" ( echo Error: Config directory does not exist \"%CONFIG_DIR%\" echo Please check CONFIG_DIR setting in batch file echo. if \"%1\"==\"\" pause goto exit_script)rem Check if source file existsif not exist \"%CONFIG_DIR%\\settings-%target%.json\" ( echo. echo Error: Cannot find config file \"settings-%target%.json\" echo Please ensure file exists in directory: %CONFIG_DIR% echo. if \"%1\"==\"\" pause goto exit_script)rem Backup current configif exist \"%CONFIG_DIR%\\settings.json\" ( copy \"%CONFIG_DIR%\\settings.json\" \"%CONFIG_DIR%\\settings.json.bak\" >nul 2>&1)rem Copy new configcopy \"%CONFIG_DIR%\\settings-%target%.json\" \"%CONFIG_DIR%\\settings.json\" >nulif %errorlevel%==0 ( echo. echo Success: Switched to \"%target%\" config echo.) else ( echo. echo Error: Failed to switch config echo. if \"%1\"==\"\" pause goto exit_script)if \"%1\"==\"\" ( echo Press any key to continue... pause >nul goto interactive)goto exit_script:show_currentecho.echo Current config file content:echo ======================================if exist \"%CONFIG_DIR%\\settings.json\" ( type \"%CONFIG_DIR%\\settings.json\") else ( echo settings.json file does not exist)echo.echo ======================================echo.if \"%1\"==\"\" ( pause goto interactive)goto exit_script:exit_scriptexit /b 0