当前位置: 首页 > Linux

使用脚本一键切换Homebrew国内镜像源

时间:2023-04-06 23:12:39 Linux

这是一个切换brew镜像的脚本,一键菜单操作。目前只增加了阿里云和中国科学技术大学的图片源。如需使用其他图片,请根据格式自行添加。步骤:新建文件changeBrewMirror.sh;将以下代码复制到文件中;赋予文件可执行权限chmodu+xchangeBrewMirror.sh;运行脚本shchangeBrewMirror.sh或./changeBrewMirror.sh;好的#!/bin/bash###################################################TODO:修改macOS系统下brew为国内镜像源#示例:##./changeBrewMirror.sh##作者:whoru.S.Q#链接:https://github.com/whorusq/learning-linux/blob/master/shell/changeBrewMirror.sh#版本: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#菜单功能菜单{#根据配置读取镜像列表,构造操作菜单localmenu_num=1localMENUS=""for((i=1;i<=${#MIRROR_LIST[@]};i++))doIFS=,localmirror=(${MIRROR_LIST[$(($i-1))]})MENUS=$MENUS[${menu_num}].${mirror[0]}镜像源\n"ALLOWED_CHOICE[i]=$menu_nummenu_num=$(($menu_num+1))doneMENUS=$MENUS"[0].恢复default\n"clearecho"------------------------------------"echo-en$MENUSIFS=$IFS_OLDecho"------------------------------------"getShellConfigPath;handleChoice;}#处理用户输入函数handleChoice{echo-en"Pleaseenter\033[32mserialnumber\033[0mtoselecttheoperationtoperform:"readchoiceif[["${ALLOWED_CHOICE[@]}"=~"$选择"]];那么如果[$choice-eq0];然后重置;否则改变$choice;fielseif[$ERROR_NO-lt$MAX_ERROR_NO];然后echo-e"无效操作,请重新输入...\n"ERROR_NO=$(($ERROR_NO+1))handleChoice;elseecho-e"错误太多,请重新运行程序"exit1fifi}#获取shell配置文件路径functiongetShellConfigPath{localshell_type=`echo$SHELL`if[["${SHELL_TYPE_LIST[@]}"=~"$shell_type"]];然后在“/bin/zsh”中输入“$shell_type”)SHELL_CONFIG_PATH=~/.zshrc;;"/bin/bash")SHELL_CONFIG_PATH=~/.bash_profile;;*)#默认;;esacelseecho-e"Unknownshelltype,pleasesetmanually"exit1fi}#显示上一步的执行结果functionshowResult{if[`echo$?`-eq0];然后echo"ok"elseecho"failed"fi}#Replace#brewconfig|grep"${mirror_config[1]}"|wc-lfunctionchange{#根据传入的数字读取对应的配置信息IFS=,localmirror_config=(${MIRROR_LIST[$(($1-1))]})#brew.gitecho-e"\n\033[32m==>\033[0m替换\033[32mbrew.git\033[0m\n"cd"$(brew--repo)"gitremoteset-urlorigin${mirror_config[1]}showResult;#homebrew-core.gitecho-e"\n\033[32m==>\033[0m替换\033[32mhomebrew-core.git\033[0m\n"cd"$(brew--repo)/图书馆/水龙头/自制软件/自制软件-core"gitremoteset-urlorigin${mirror_config[2]}showResult;#updateecho-e"\n\033[32m==>\033[0mupdate\033[32mbrew\033[0m\n"brewupdateshowResult;#homebrew-bottlesecho-e"\n\033[32m==>\033[0m替换\033[32mhomebrew-bottles\033[0m\n"localexp="exportHOMEBREW_BOTTLE_DOMAIN=${mirror_config[3]}"if[$SHELL_CONFIG_PATH!=""];thenecho$exp>>$SHELL_CONFIG_PATHsource$SHELL_CONFIG_PATH>/dev/null2>&1elseecho-e"找不到shell配置文件,请手动设置$exp添加到你系统的环境变量中"exit1fishowResult;echo-e"\n成功切换到【${mirror_config[0]}】镜像源\n"}#restorefunctionreset{echo-e"\n\033[32m==>\033[0mrestore\033[32mbrew.git\033[0m\n"cd"$(brew--repo)"gitremoteset-urloriginhttps://github.com/Homebrew/brew.gitshowResult;echo-e"\n\033[32m==>\033[0mrestore\033[32mhomebrew-core.git\033[0m\n"cd"$(brew--repo)/Library/Taps/homebrew/homebrew-core"gitremoteset-urloriginhttps://github.com/Homebrew/homebrew-core.gitshowResult;echo-e"\n\033[32m==>\033[0mupdate\033[32mbrew\033[0m\n"brewupdateshowResult;echo-e"\n\033[32m==>\033[0mrestore\033[32mhomebrew-bottles\033[0m\n"sed-e'/HOMEBREW_BOTTLE_DOMAIN/d'$SHELL_CONFIG_PATH>/dev/null2>&1source$SHELL_CONFIG_PATH>/dev/null2>&1showResult;echo-e"\nRestored\n"}menu;