# 判断是Linux还是Mac os OS="$(uname)" if [[ "$OS" == "Linux" ]]; then HOMEBREW_ON_LINUX=1 elif [[ "$OS" != "Darwin" ]]; then echo"Homebrew 只运行在 Mac OS 或 Linux." fi
#设置一些平台地址 if [[ -z "${HOMEBREW_ON_LINUX-}" ]]; then #Mac if [[ "$UNAME_MACHINE" == "arm64" ]]; then #M1 HOMEBREW_PREFIX="/opt/homebrew" HOMEBREW_REPOSITORY="${HOMEBREW_PREFIX}" else #Inter HOMEBREW_PREFIX="/usr/local" HOMEBREW_REPOSITORY="${HOMEBREW_PREFIX}/Homebrew" fi
#创建brew需要的目录 直接复制于国外版本,同步 CreateBrewLinkFolder() { echo"--创建Brew所需要的目录" directories=(bin etc include lib sbin share opt var Frameworks etc/bash_completion.d lib/pkgconfig share/aclocal share/doc share/info share/locale share/man share/man/man1 share/man/man2 share/man/man3 share/man/man4 share/man/man5 share/man/man6 share/man/man7 share/man/man8 var/log var/homebrew var/homebrew/linked bin/brew) group_chmods=() fordirin"${directories[@]}"; do if exists_but_not_writable "${HOMEBREW_PREFIX}/${dir}"; then group_chmods+=("${HOMEBREW_PREFIX}/${dir}") fi done
directories=(share/zsh share/zsh/site-functions) zsh_dirs=() fordirin"${directories[@]}"; do zsh_dirs+=("${HOMEBREW_PREFIX}/${dir}") done
directories=(bin etc include lib sbin share var opt share/zsh share/zsh/site-functions var/homebrew var/homebrew/linked Cellar Caskroom Frameworks) mkdirs=() fordirin"${directories[@]}"; do if ! [[ -d "${HOMEBREW_PREFIX}/${dir}" ]]; then mkdirs+=("${HOMEBREW_PREFIX}/${dir}") fi done
user_chmods=() if [[ "${#zsh_dirs[@]}" -gt 0 ]]; then fordirin"${zsh_dirs[@]}"; do if user_only_chmod "${dir}"; then user_chmods+=("${dir}") fi done fi
chmods=() if [[ "${#group_chmods[@]}" -gt 0 ]]; then chmods+=("${group_chmods[@]}") fi if [[ "${#user_chmods[@]}" -gt 0 ]]; then chmods+=("${user_chmods[@]}") fi
chowns=() chgrps=() if [[ "${#chmods[@]}" -gt 0 ]]; then fordirin"${chmods[@]}"; do if file_not_owned "${dir}"; then chowns+=("${dir}") fi if file_not_grpowned "${dir}"; then chgrps+=("${dir}") fi done fi
if [[ -d "${HOMEBREW_PREFIX}" ]]; then if [[ "${#chmods[@]}" -gt 0 ]]; then execute_sudo "/bin/chmod""u+rwx""${chmods[@]}" fi if [[ "${#group_chmods[@]}" -gt 0 ]]; then execute_sudo "/bin/chmod""g+rwx""${group_chmods[@]}" fi if [[ "${#user_chmods[@]}" -gt 0 ]]; then execute_sudo "/bin/chmod""755""${user_chmods[@]}" fi if [[ "${#chowns[@]}" -gt 0 ]]; then execute_sudo "$CHOWN""$USER""${chowns[@]}" fi if [[ "${#chgrps[@]}" -gt 0 ]]; then execute_sudo "$CHGRP""$GROUP""${chgrps[@]}" fi else execute_sudo "/bin/mkdir""-p""${HOMEBREW_PREFIX}" if [[ -z "${HOMEBREW_ON_LINUX-}" ]]; then execute_sudo "$CHOWN""root:wheel""${HOMEBREW_PREFIX}" else execute_sudo "$CHOWN""$USER:$GROUP""${HOMEBREW_PREFIX}" fi fi
if [[ "${#mkdirs[@]}" -gt 0 ]]; then execute_sudo "/bin/mkdir""-p""${mkdirs[@]}" execute_sudo "/bin/chmod""g+rwx""${mkdirs[@]}" execute_sudo "$CHOWN""$USER""${mkdirs[@]}" execute_sudo "$CHGRP""$GROUP""${mkdirs[@]}" fi
if ! [[ -d "${HOMEBREW_REPOSITORY}" ]]; then execute_sudo "/bin/mkdir""-p""${HOMEBREW_REPOSITORY}" fi execute_sudo "$CHOWN""-R""$USER:$GROUP""${HOMEBREW_REPOSITORY}"
if ! [[ -d "${HOMEBREW_CACHE}" ]]; then if [[ -z "${HOMEBREW_ON_LINUX-}" ]]; then execute_sudo "/bin/mkdir""-p""${HOMEBREW_CACHE}" else execute "/bin/mkdir""-p""${HOMEBREW_CACHE}" fi fi if exists_but_not_writable "${HOMEBREW_CACHE}"; then execute_sudo "/bin/chmod""g+rwx""${HOMEBREW_CACHE}" fi if file_not_owned "${HOMEBREW_CACHE}"; then execute_sudo "$CHOWN""-R""$USER""${HOMEBREW_CACHE}" fi if file_not_grpowned "${HOMEBREW_CACHE}"; then execute_sudo "$CHGRP""-R""$GROUP""${HOMEBREW_CACHE}" fi if [[ -d "${HOMEBREW_CACHE}" ]]; then execute "$TOUCH""${HOMEBREW_CACHE}/.cleaned" fi echo"--依赖目录脚本运行完成" }
git config --global --unset http.proxy${tty_reset} " fi }
echo" ${tty_green} 开始执行Brew自动安装程序 ${tty_reset} ${tty_cyan} [cunkai.wang@foxmail.com] ${tty_reset} ['$TIME']['$macos_version'] ${tty_cyan} https://zhuanlan.zhihu.com/p/111014448 ${tty_reset} " #选择一个下载源 echo -n "${tty_green} 请选择一个下载镜像,例如中科大,输入1回车。 源有时候不稳定,如果git克隆报错重新运行脚本选择源。cask非必须,有部分人需要。 1、中科大下载源 2、清华大学下载源 3、北京外国语大学下载源 ${tty_reset}" if [[ -z "${HOMEBREW_ON_LINUX-}" ]]; then #mac才显示腾讯 阿里,他们对linux目前支持很差 echo"${tty_green} 4、腾讯下载源(不显示下载进度) 5、阿里巴巴下载源(缺少cask源) ${tty_reset} " fi echo -n " ${tty_blue}请输入序号: " read MY_DOWN_NUM echo"${tty_reset}" case$MY_DOWN_NUMin "2") echo" 你选择了清华大学下载源 " if [[ -z "${HOMEBREW_ON_LINUX-}" ]]; then USER_HOMEBREW_BOTTLE_DOMAIN=https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles else USER_HOMEBREW_BOTTLE_DOMAIN=https://mirrors.tuna.tsinghua.edu.cn/linuxbrew-bottles/ fi #HomeBrew基础框架 USER_BREW_GIT=https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git #HomeBrew Core if [[ -z "${HOMEBREW_ON_LINUX-}" ]]; then USER_CORE_GIT=https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git else USER_CORE_GIT=https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/linuxbrew-core.git fi #HomeBrew Cask USER_CASK_GIT=https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-cask.git USER_CASK_FONTS_GIT=https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-cask-fonts.git USER_CASK_DRIVERS_GIT=https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-cask-drivers.git ;; "3") echo" 北京外国语大学下载源 " if [[ -z "${HOMEBREW_ON_LINUX-}" ]]; then USER_HOMEBREW_BOTTLE_DOMAIN=https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles else USER_HOMEBREW_BOTTLE_DOMAIN=https://mirrors.tuna.tsinghua.edu.cn/linuxbrew-bottles/ fi #HomeBrew基础框架 USER_BREW_GIT=https://mirrors.bfsu.edu.cn/git/homebrew/brew.git #HomeBrew Core if [[ -z "${HOMEBREW_ON_LINUX-}" ]]; then USER_CORE_GIT=https://mirrors.bfsu.edu.cn/git/homebrew/homebrew-core.git else USER_CORE_GIT=https://mirrors.bfsu.edu.cn/git/homebrew/linuxbrew-core.git fi #HomeBrew Cask USER_CASK_GIT=https://mirrors.bfsu.edu.cn/git/homebrew/homebrew-cask.git USER_CASK_FONTS_GIT=https://mirrors.bfsu.edu.cn/git/homebrew/homebrew-cask-fonts.git USER_CASK_DRIVERS_GIT=https://mirrors.bfsu.edu.cn/git/homebrew/homebrew-cask-drivers.git ;; "4") echo" 你选择了腾讯下载源 " USER_HOMEBREW_BOTTLE_DOMAIN=https://mirrors.cloud.tencent.com/homebrew-bottles #HomeBrew基础框架 USER_BREW_GIT=https://mirrors.cloud.tencent.com/homebrew/brew.git #HomeBrew Core if [[ -z "${HOMEBREW_ON_LINUX-}" ]]; then USER_CORE_GIT=https://mirrors.cloud.tencent.com/homebrew/homebrew-core.git else USER_CORE_GIT=https://mirrors.cloud.tencent.com/homebrew/linuxbrew-core.git fi #HomeBrew Cask USER_CASK_GIT=https://mirrors.cloud.tencent.com/homebrew/homebrew-cask.git ;; "5") echo" 你选择了阿里巴巴下载源(无mac的cask源,无Linux版本) " USER_HOMEBREW_BOTTLE_DOMAIN=https://mirrors.aliyun.com/homebrew/homebrew-bottles #HomeBrew基础框架 USER_BREW_GIT=https://mirrors.aliyun.com/homebrew/brew.git #HomeBrew Core if [[ -z "${HOMEBREW_ON_LINUX-}" ]]; then USER_CORE_GIT=https://mirrors.aliyun.com/homebrew/homebrew-core.git else USER_CORE_GIT=https://mirrors.ustc.edu.cn/linuxbrew-core.git echo"阿里巴巴无core,这里替换为了中国科学技术大学的linuxbrew-core" fi #HomeBrew Cask USER_CASK_GIT=https://mirrors.aliyun.com/homebrew/homebrew-cask.git ;; *) echo" 你选择了中国科学技术大学下载源 " #HomeBrew 下载源 install if [[ -z "${HOMEBREW_ON_LINUX-}" ]]; then USER_HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles else USER_HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/linuxbrew-bottles/ fi #HomeBrew基础框架 USER_BREW_GIT=https://mirrors.ustc.edu.cn/brew.git #HomeBrew Core if [[ -z "${HOMEBREW_ON_LINUX-}" ]]; then USER_CORE_GIT=https://mirrors.ustc.edu.cn/homebrew-core.git else USER_CORE_GIT=https://mirrors.ustc.edu.cn/linuxbrew-core.git fi #HomeBrew Cask USER_CASK_GIT=https://mirrors.ustc.edu.cn/homebrew-cask.git ;; esac echo -n "${tty_green}!!!此脚本将要删除之前的brew(包括它下载的软件),请自行备份。 ->是否现在开始执行脚本(N/Y) " read MY_Del_Old echo"${tty_reset}" case$MY_Del_Oldin "y") echo"--> 脚本开始执行" ;; "Y") echo"--> 脚本开始执行" ;; *) echo"你输入了 $MY_Del_Old ,自行备份老版brew和它下载的软件, 如果继续运行脚本应该输入Y或者y " exit 0 ;; esac
if [[ -z "${HOMEBREW_ON_LINUX-}" ]]; then #MAC echo"${tty_yellow} Mac os设置开机密码方法: (设置开机密码:在左上角苹果图标->系统偏好设置->"用户与群组"->更改密码) (如果提示This incident will be reported. 在"用户与群组"中查看是否管理员) ${tty_reset}" fi
# 让环境暂时纯粹,脚本运行结束后恢复 if [[ -z "${HOMEBREW_ON_LINUX-}" ]]; then export PATH=/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:${HOMEBREW_REPOSITORY}/bin fi git --version if [ $? -ne 0 ];then
if [[ -z "${HOMEBREW_ON_LINUX-}" ]]; then sudorm -rf "/Library/Developer/CommandLineTools/" echo"${tty_cyan}安装Git${tty_reset}后再运行此脚本,${tty_red}在系统弹窗中点击“安装”按钮 如果没有弹窗的老系统,需要自己下载安装:https://sourceforge.net/projects/git-osx-installer/ ${tty_reset}" xcode-select --install exit 0 else echo"${tty_red} 发现缺少git,开始安装,请输入Y ${tty_reset}" sudo apt install git fi fi
if [[ -z "${HOMEBREW_ON_LINUX-}" ]]; then #MAC echo"==> 克隆Homebrew Cask 图形化软件 ${tty_cyan}此处如果显示Password表示需要再次输入开机密码,输入完后回车${tty_reset}" if [[ "$MY_DOWN_NUM" -eq "5" ]];then echo"${tty_yellow} 阿里源没有Cask 跳过${tty_reset}" else sudomkdir -p ${HOMEBREW_REPOSITORY}/Library/Taps/homebrew/homebrew-cask sudo git clone${GIT_SPEED}$USER_CASK_GIT${HOMEBREW_REPOSITORY}/Library/Taps/homebrew/homebrew-cask/ if [ $? -ne 0 ];then sudorm -rf ${HOMEBREW_REPOSITORY}/Library/Taps/homebrew/homebrew-cask echo"${tty_red}尝试切换下载源或者切换网络,不过Cask组件非必须模块。可以忽略${tty_reset}" else echo"${tty_green}此步骤成功${tty_reset}"
fi fi else #Linux echo"${tty_yellow} Linux 不支持Cask图形化软件下载 此步骤跳过${tty_reset}" fi echo'==> 配置国内镜像源HOMEBREW BOTTLE'
#判断下终端是Bash还是zsh case"$SHELL"in */bash*) if [[ -r "$HOME/.bash_profile" ]]; then shell_profile="${HOME}/.bash_profile" else shell_profile="${HOME}/.profile" fi ;; */zsh*) shell_profile="${HOME}/.zprofile" ;; *) shell_profile="${HOME}/.profile" ;; esac
if [[ -f ${shell_profile} ]]; then AddPermission ${shell_profile} fi #删除之前的环境变量 if [[ -z "${HOMEBREW_ON_LINUX-}" ]]; then #Mac sed -i """/ckbrew/d"${shell_profile} else #Linux sed -i "/ckbrew/d"${shell_profile} fi #写入环境变量到文件 echo"环境变量写入->${shell_profile}"
if [[ -n "${HOMEBREW_ON_LINUX-}" ]]; then #检测linux curl是否有安装 echo"-检测curl是否安装" curl -V if [ $? -ne 0 ];then sudo apt-get install curl if [ $? -ne 0 ];then sudo yum install curl if [ $? -ne 0 ];then echo'失败 请自行安装curl 可以参考https://www.howtoing.com/install-curl-in-linux' error_game_over fi fi fi fi
brew -v if [ $? -ne 0 ];then echo'发现错误,自动修复一次!' rm -rf $HOMEBREW_CACHE export PATH=/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:${HOMEBREW_REPOSITORY}/bin brew update-reset brew -v if [ $? -ne 0 ];then error_game_over fi else echo"${tty_green}Brew前期配置成功${tty_reset}" fi echo' ==> brew update ' HOMEBREW_BOTTLE_DOMAIN=${USER_HOMEBREW_BOTTLE_DOMAIN} if [[ $GIT_SPEED == "" ]];then brew update if [[ $? -ne 0 ]];then error_game_over exit 0 fi else #极速模式提示Update修复方法 echo"${tty_green} 极速版本安装完成,${tty_reset} install功能正常,如果需要update功能请自行运行下面两句命令 git -C ${HOMEBREW_REPOSITORY}/Library/Taps/homebrew/homebrew-core fetch --unshallow git -C ${HOMEBREW_REPOSITORY}/Library/Taps/homebrew/homebrew-cask fetch --unshallow " fi