這是一個切換 brew 鏡像的腳本,菜單一鍵操做。目前只添加了 阿里雲和 中科大的鏡像源,若是須要使用其它鏡像,請按照格式自行添加。
步驟:linux
changeBrewMirror.sh
;chmod u+x changeBrewMirror.sh
;sh changeBrewMirror.sh
或 ./changeBrewMirror.sh
;<!-- more -->git
#!/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 ;