这是一个切换 brew 镜像的脚本,菜单一键操作。目前只增加了 阿里云 和中科大 的镜像源,如果须要应用其它镜像,请依照格局自行添加。
步骤:
- 新建文件
changeBrewMirror.sh
; - 将上面的代码拷贝到文件中;
- 给文件赋予可执行权限
chmod u+x changeBrewMirror.sh
; - 运行脚本
sh changeBrewMirror.sh
或./changeBrewMirror.sh
; - ok
<!– more –>
#!/bin/bash
################################################
# TODO: 批改 macOS 零碎下的 brew 为国内镜像源
# 示例:#
# ./changeBrewMirror.sh
#
# Author: whoru.S.Q <whoru@sqiang.net>
# Link: https://github.com/whorusq/learning-linux/blob/master/shell/changeBrewMirror.sh
# Version: 1.0
################################################
# 镜像列表
# 格局:"镜像名称,brew 地址,homebrew-core 地址,homebrew-bottles 地址"
MIRROR_LIST=(
"阿里云,https://mirrors.aliyun.com/homebrew/brew.git,https://mirrors.aliyun.com/homebrew/homebrew-core.git,https://mirrors.aliyun.com/homebrew/homebrew-bottles"
"中科院,https://mirrors.ustc.edu.cn/brew.git,https://mirrors.ustc.edu.cn/homebrew-core.git,https://mirrors.ustc.edu.cn/homebrew-bottles"
)
IFS_OLD=$IFS
# 反对的 shell 类型
# 其它类型请批改相干判断逻辑
SHELL_TYPE_LIST=("/bin/zsh" "/bin/bash")
# 以后 shell 的配置文件门路
SHELL_CONFIG_PATH=""
# 容许的操作序号
ALLOWED_CHOICE=(0)
# 输出谬误计数
ERROR_NO=0
# 最大谬误输出次数
MAX_ERROR_NO=3
# 菜单
function menu {
# 依据配置读取镜像列表,结构操作菜单
local menu_num=1
local MENUS=""
for((i=1; i<=${#MIRROR_LIST[@]}; i++))
do
IFS=,
local mirror=(${MIRROR_LIST[$(($i-1))]})
MENUS=$MENUS"[${menu_num}]. ${mirror[0]}镜像源 \n"
ALLOWED_CHOICE[i]=$menu_num
menu_num=$(($menu_num+1))
done
MENUS=$MENUS"[0]. 复原默认 \n"
clear
echo "-------------------------------------"
echo -en $MENUS
IFS=$IFS_OLD
echo "-------------------------------------"
getShellConfigPath ;
handleChoice ;
}
# 解决用户输出
function handleChoice {
echo -en "请输出 \033[32m 序号 \033[0m 抉择要执行的操作:"
read choice
if [["${ALLOWED_CHOICE[@]}" =~ "$choice" ]]; then
if [$choice -eq 0]; then
reset ;
else
change $choice;
fi
else
if [$ERROR_NO -lt $MAX_ERROR_NO]; then
echo -e "有效操作,请从新输出...\n"
ERROR_NO=$(($ERROR_NO+1))
handleChoice ;
else
echo -e "谬误次数过多,请从新运行程序"
exit 1
fi
fi
}
# 获取 shell 配置文件门路
function getShellConfigPath {
local shell_type=`echo $SHELL`
if [["${SHELL_TYPE_LIST[@]}" =~ "$shell_type" ]]; then
case "$shell_type" in
"/bin/zsh")
SHELL_CONFIG_PATH=~/.zshrc
;;
"/bin/bash")
SHELL_CONFIG_PATH=~/.bash_profile
;;
*)
# default
;;
esac
else
echo -e "未知的 shell 类型,请手动设置"
exit 1
fi
}
# 显示上一步执行后果
function showResult {if [ `echo $?` -eq 0]; then
echo "ok"
else
echo "failed"
fi
}
# 替换
# brew config | grep "${mirror_config[1]}" | wc -l
function change {
# 依据传过来的编号读取对应的配置信息
IFS=,
local mirror_config=(${MIRROR_LIST[$(($1-1))]})
# brew.git
echo -e "\n\033[32m==>\033[0m 替换 \033[32m brew.git \033[0m\n"
cd "$(brew --repo)"
git remote set-url origin ${mirror_config[1]}
showResult ;
# homebrew-core.git
echo -e "\n\033[32m==>\033[0m 替换 \033[32m homebrew-core.git \033[0m\n"
cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core"
git remote set-url origin ${mirror_config[2]}
showResult ;
# 更新
echo -e "\n\033[32m==>\033[0m 更新 \033[32m brew \033[0m\n"
brew update
showResult ;
# homebrew-bottles
echo -e "\n\033[32m==>\033[0m 替换 \033[32m homebrew-bottles \033[0m\n"
local exp="export HOMEBREW_BOTTLE_DOMAIN=${mirror_config[3]}"
if [$SHELL_CONFIG_PATH != ""]; then
echo $exp >> $SHELL_CONFIG_PATH
source $SHELL_CONFIG_PATH >/dev/null 2>&1
else
echo -e "找不到 shell 配置文件,请手动将 $exp 增加到你零碎的环境变量中。"
exit 1
fi
showResult ;
echo -e "\n 胜利切换到【${mirror_config[0]}】镜像源 \n"
}
# 复原
function reset {
echo -e "\n\033[32m==>\033[0m 复原 \033[32m brew.git \033[0m\n"
cd "$(brew --repo)"
git remote set-url origin https://github.com/Homebrew/brew.git
showResult ;
echo -e "\n\033[32m==>\033[0m 复原 \033[32m homebrew-core.git \033[0m\n"
cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core"
git remote set-url origin https://github.com/Homebrew/homebrew-core.git
showResult ;
echo -e "\n\033[32m==>\033[0m 更新 \033[32m brew \033[0m\n"
brew update
showResult ;
echo -e "\n\033[32m==>\033[0m 复原 \033[32m homebrew-bottles \033[0m\n"
sed -e '/HOMEBREW_BOTTLE_DOMAIN/d' $SHELL_CONFIG_PATH >/dev/null 2>&1
source $SHELL_CONFIG_PATH >/dev/null 2>&1
showResult ;
echo -e "\n 已复原 \n"
}
menu ;