スクリプトワンタッチでHomebrewの国内ミラーソースを切り替えます
これはbrewミラーを切り替えるスクリプトで、メニューのキー操作です.現在追加されているのは
アリ雲和
中科大のミラーソースは、他のミラーを使用する必要がある場合は、フォーマットに従って自分で追加します.
手順:新規ファイル 次のコードをファイルにコピーします. は、ファイルに実行可能な権限 スクリプト ok
アリ雲和
中科大のミラーソースは、他のミラーを使用する必要がある場合は、フォーマットに従って自分で追加します.
手順:
changeBrewMirror.sh
;chmod u+x changeBrewMirror.sh
を付与する.sh changeBrewMirror.sh
または./changeBrewMirror.sh
を実行します.#!/bin/bash
################################################
# TODO: macOS brew
# :
#
# ./changeBrewMirror.sh
#
# Author: whoru.S.Q
# 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]}
"
ALLOWED_CHOICE[i]=$menu_num
menu_num=$(($menu_num+1))
done
MENUS=$MENUS"[0].
"
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 " , ...
"
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 "
\033[32m==>\033[0m \033[32m brew.git \033[0m
"
cd "$(brew --repo)"
git remote set-url origin ${mirror_config[1]}
showResult ;
# homebrew-core.git
echo -e "
\033[32m==>\033[0m \033[32m homebrew-core.git \033[0m
"
cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core"
git remote set-url origin ${mirror_config[2]}
showResult ;
#
echo -e "
\033[32m==>\033[0m \033[32m brew \033[0m
"
brew update
showResult ;
# homebrew-bottles
echo -e "
\033[32m==>\033[0m \033[32m homebrew-bottles \033[0m
"
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 "
【${mirror_config[0]}】
"
}
#
function reset {
echo -e "
\033[32m==>\033[0m \033[32m brew.git \033[0m
"
cd "$(brew --repo)"
git remote set-url origin https://github.com/Homebrew/brew.git
showResult ;
echo -e "
\033[32m==>\033[0m \033[32m homebrew-core.git \033[0m
"
cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core"
git remote set-url origin https://github.com/Homebrew/homebrew-core.git
showResult ;
echo -e "
\033[32m==>\033[0m \033[32m brew \033[0m
"
brew update
showResult ;
echo -e "
\033[32m==>\033[0m \033[32m homebrew-bottles \033[0m
"
sed -e '/HOMEBREW_BOTTLE_DOMAIN/d' $SHELL_CONFIG_PATH >/dev/null 2>&1
source $SHELL_CONFIG_PATH >/dev/null 2>&1
showResult ;
echo -e "
"
}
menu ;