脚本概述
这是一个用于管理 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 或 ~/.zshrc
alias 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 off
setlocal enabledelayedexpansion
rem Config Directory Setting
set "CONFIG_DIR=C:\Users\muxiongxiong\.claude"
rem Check if parameter provided
if "%1"=="" goto interactive
rem Direct parameter mode
set "choice=%1"
goto process_choice
:interactive
echo.
echo ======================================
echo Config Switch Tool
echo ======================================
echo.
echo Please select config to switch:
echo.
echo [1] any - Switch to settings-any.json
echo [2] yes - Switch to settings-yes.json
echo [3] pack - Switch to settings-pack.json
echo [4] Show current config
echo [0] Exit
echo.
set /p "choice=Enter option (1-4, 0 to exit): "
:process_choice
rem Process choice
if "%choice%"=="1" set "target=any"
if "%choice%"=="2" set "target=yes"
if "%choice%"=="3" set "target=pack"
if "%choice%"=="4" goto show_current
if "%choice%"=="0" goto exit_script
if "%choice%"=="any" set "target=any"
if "%choice%"=="yes" set "target=yes"
if "%choice%"=="pack" set "target=pack"
rem Check if target is set
if not defined target (
echo.
echo Error: Invalid option "%choice%"
echo.
if "%1"=="" (
pause
goto interactive
) else (
goto exit_script
)
)
rem Execute config switch
goto switch_config
:switch_config
rem Show current config directory
echo Config directory: %CONFIG_DIR%
echo.
rem Check if config directory exists
if 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 exists
if 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 config
if exist "%CONFIG_DIR%\settings.json" (
copy "%CONFIG_DIR%\settings.json" "%CONFIG_DIR%\settings.json.bak" >nul 2>&1
)
rem Copy new config
copy "%CONFIG_DIR%\settings-%target%.json" "%CONFIG_DIR%\settings.json" >nul
if %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_current
echo.
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_script
exit /b 0