前言
來啦老鐵!
又到學知識的時候啦瓢剿,原本想搞一下 STF 的逢慌,結(jié)果環(huán)境沒搞定,STF 一直安裝不上间狂,為了不讓今天的知識流產(chǎn)攻泼,翻了一下小本本,那就記點有用的小知識吧鉴象!
- HomeBrew自動安裝腳本
1. 背景忙菠;
筆者用了很久很久的 windows 機器,最近一兩年才開始使用 Mac纺弊,很多也不是很熟悉牛欢,剛接觸 Mac 本時,同事們教我用 HomeBrew 安裝一些開發(fā)常用軟件淆游,比較方便傍睹,比如:
- 安裝 nodejs:brew install nodejs;
- 安裝 python3:brew install python3犹菱;
- 安裝 ffmpeg:brew install ffmpeg拾稳;
- 等很多軟件;
而想要使用 brew install 命令已亥,前提是要安裝 HomeBrew熊赖,依稀記得當初的執(zhí)行命令是:
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
但很容易發(fā)現(xiàn)来屠,想從這個站點安裝 HomeBrew 或 nvm 等軟件虑椎,會報這樣的錯誤:
記得是需要翻墻,賊麻煩呀俱笛,后來發(fā)現(xiàn)了個好貨~
2. HomeBrew自動安裝腳本捆姜;
至于腳本的來源,筆者已無從考起迎膜,就用吧泥技,香就得了~
步驟:
- 新建任意 .sh 文件,例如:Homebrew.sh磕仅;
- 復制以下腳本珊豹,粘貼至 Homebrew.sh 文件簸呈;
- 保存 Homebrew.sh 并執(zhí)行 Homebrew.sh 文件(執(zhí)行命令:sh Homebrew.sh),按提示操作即可店茶;
#HomeBrew自動安裝腳本
#cunkai.wang@foxmail.com
#獲取硬件信息
UNAME_MACHINE="$(uname -m)"
#在X86電腦上測試arm電腦
# UNAME_MACHINE="arm64"
# 判斷是Linux還是Mac os
OS="$(uname)"
if [[ "$OS" == "Linux" ]]; then
HOMEBREW_ON_LINUX=1
elif [[ "$OS" != "Darwin" ]]; then
echo "Homebrew 只運行在 Mac OS 或 Linux."
fi
# 字符串染色程序
if [[ -t 1 ]]; then
tty_escape() { printf "\033[%sm" "$1"; }
else
tty_escape() { :; }
fi
tty_universal() { tty_escape "0;$1"; } #正常顯示
tty_mkbold() { tty_escape "1;$1"; } #設置高亮
tty_underline="$(tty_escape "4;39")" #下劃線
tty_blue="$(tty_universal 34)" #藍色
tty_red="$(tty_universal 31)" #紅色
tty_green="$(tty_universal 32)" #綠色
tty_yellow="$(tty_universal 33)" #黃色
tty_bold="$(tty_universal 39)" #加黑
tty_cyan="$(tty_universal 36)" #青色
tty_reset="$(tty_escape 0)" #去除顏色
#用戶輸入極速安裝speed蜕便,git克隆只取最近新版本
#但是update會出錯,提示需要下載全部數(shù)據(jù)
GIT_SPEED=""
if [[ $0 == "speed" ]]; then
GIT_SPEED="--depth=1"
else
for dir in $@; do
echo $dir
if [[ $dir == "speed" ]]; then
GIT_SPEED="--depth=1"
fi
done
fi
if [[ $GIT_SPEED != "" ]]; then
echo "${tty_red}
檢測到參數(shù)speed贩幻,只拉取最新數(shù)據(jù)轿腺,可以正常install使用!
但是以后brew update的時候會報錯丛楚,運行報錯提示的兩句命令即可修復
${tty_reset}"
fi
#獲取前面兩個.的數(shù)據(jù)
major_minor() {
echo "${1%%.*}.$(x="${1#*.}"; echo "${x%%.*}")"
}
#設置一些平臺地址
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
HOMEBREW_CACHE="${HOME}/Library/Caches/Homebrew"
HOMEBREW_LOGS="${HOME}/Library/Logs/Homebrew"
#國內(nèi)沒有homebrew-services族壳,手動在gitee創(chuàng)建了一個,有少數(shù)人用到趣些。
USER_SERVICES_GIT=https://gitee.com/cunkai/homebrew-services.git
STAT="stat -f"
CHOWN="/usr/sbin/chown"
CHGRP="/usr/bin/chgrp"
GROUP="admin"
TOUCH="/usr/bin/touch"
#獲取Mac系統(tǒng)版本
macos_version="$(major_minor "$(/usr/bin/sw_vers -productVersion)")"
else
#Linux
UNAME_MACHINE="$(uname -m)"
HOMEBREW_PREFIX="/home/linuxbrew/.linuxbrew"
HOMEBREW_REPOSITORY="${HOMEBREW_PREFIX}/Homebrew"
HOMEBREW_CACHE="${HOME}/.cache/Homebrew"
HOMEBREW_LOGS="${HOME}/.logs/Homebrew"
STAT="stat --printf"
CHOWN="/bin/chown"
CHGRP="/bin/chgrp"
GROUP="$(id -gn)"
TOUCH="/bin/touch"
fi
#獲取系統(tǒng)時間
TIME=$(date "+%Y-%m-%d %H:%M:%S")
JudgeSuccess()
{
if [ $? -ne 0 ];then
echo "${tty_red}m此步驟失敗 '$1'${tty_reset}"
if [[ "$2" == 'out' ]]; then
exit 0
fi
else
echo "${tty_green}此步驟成功${tty_reset}"
fi
}
# 判斷是否有系統(tǒng)權(quán)限
have_sudo_access() {
if [[ -z "${HAVE_SUDO_ACCESS-}" ]]; then
/usr/bin/sudo -l mkdir &>/dev/null
HAVE_SUDO_ACCESS="$?"
fi
if [[ "$HAVE_SUDO_ACCESS" -ne 0 ]]; then
echo "${tty_red}開機密碼輸入錯誤仿荆,獲取權(quán)限失敗!${tty_reset}"
fi
return "$HAVE_SUDO_ACCESS"
}
abort() {
printf "%s\n" "$1"
# exit 1
}
shell_join() {
local arg
printf "%s" "$1"
shift
for arg in "$@"; do
printf " "
printf "%s" "${arg// /\ }"
done
}
execute() {
if ! "$@"; then
abort "$(printf "${tty_red}此命令運行失敗: %s${tty_reset}" "$(shell_join "$@")")"
fi
}
ohai() {
printf "${tty_blue}運行代碼 ==>${tty_bold} %s${tty_reset}\n" "$(shell_join "$@")"
}
# 管理員運行
execute_sudo()
{
local -a args=("$@")
if have_sudo_access; then
if [[ -n "${SUDO_ASKPASS-}" ]]; then
args=("-A" "${args[@]}")
fi
ohai "/usr/bin/sudo" "${args[@]}"
execute "/usr/bin/sudo" "${args[@]}"
else
ohai "${args[@]}"
execute "${args[@]}"
fi
}
#添加文件夾權(quán)限
AddPermission()
{
execute_sudo "/bin/chmod" "-R" "a+rwx" "$1"
execute_sudo "$CHOWN" "$USER" "$1"
execute_sudo "$CHGRP" "$GROUP" "$1"
}
#創(chuàng)建文件夾
CreateFolder()
{
echo '-> 創(chuàng)建文件夾' $1
execute_sudo "/bin/mkdir" "-p" "$1"
JudgeSuccess
AddPermission $1
}
RmAndCopy()
{
if [[ -d $1 ]]; then
echo " ---備份要刪除的$1到系統(tǒng)桌面...."
if ! [[ -d $HOME/Desktop/Old_Homebrew/$TIME/$1 ]]; then
sudo mkdir -p "$HOME/Desktop/Old_Homebrew/$TIME/$1"
fi
sudo cp -rf $1 "$HOME/Desktop/Old_Homebrew/$TIME/$1"
echo " ---$1 備份完成"
fi
sudo rm -rf $1
}
RmCreate()
{
RmAndCopy $1
CreateFolder $1
}
#判斷文件夾存在但不可寫
exists_but_not_writable() {
[[ -e "$1" ]] && ! [[ -r "$1" && -w "$1" && -x "$1" ]]
}
#文件所有者
get_owner() {
$(shell_join "$STAT %u $1" )
}
#文件本人無權(quán)限
file_not_owned() {
[[ "$(get_owner "$1")" != "$(id -u)" ]]
}
#獲取所屬的組
get_group() {
$(shell_join "$STAT %g $1" )
}
#不在所屬組
file_not_grpowned() {
[[ " $(id -G "$USER") " != *" $(get_group "$1") "* ]]
}
#獲得當前文件夾權(quán)限 例如777
get_permission() {
$(shell_join "$STAT %A $1" )
}
#授權(quán)當前用戶權(quán)限
user_only_chmod() {
[[ -d "$1" ]] && [[ "$(get_permission "$1")" != "755" ]]
}
#創(chuàng)建brew需要的目錄 直接復制于國外版本,同步
CreateBrewLinkFolder()
{
echo "--創(chuàng)建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=()
for dir in "${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=()
for dir in "${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=()
for dir in "${directories[@]}"; do
if ! [[ -d "${HOMEBREW_PREFIX}/${dir}" ]]; then
mkdirs+=("${HOMEBREW_PREFIX}/${dir}")
fi
done
user_chmods=()
if [[ "${#zsh_dirs[@]}" -gt 0 ]]; then
for dir in "${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
for dir in "${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提交
git_commit(){
git add .
git commit -m "your del"
}
#version_gt 判斷$1是否大于$2
version_gt() {
[[ "${1%.*}" -gt "${2%.*}" ]] || [[ "${1%.*}" -eq "${2%.*}" && "${1#*.}" -gt "${2#*.}" ]]
}
#version_ge 判斷$1是否大于等于$2
version_ge() {
[[ "${1%.*}" -gt "${2%.*}" ]] || [[ "${1%.*}" -eq "${2%.*}" && "${1#*.}" -ge "${2#*.}" ]]
}
#version_lt 判斷$1是否小于$2
version_lt() {
[[ "${1%.*}" -lt "${2%.*}" ]] || [[ "${1%.*}" -eq "${2%.*}" && "${1#*.}" -lt "${2#*.}" ]]
}
#發(fā)現(xiàn)錯誤 關閉腳本 提示如何解決
error_game_over(){
echo "
${tty_red}失敗$MY_DOWN_NUM 右鍵下面地址查看常見錯誤解決辦法
https://gitee.com/cunkai/HomebrewCN/blob/master/error.md
如果沒有解決坏平,把全部運行過程截圖發(fā)到 cunkai.wang@foxmail.com ${tty_reset}
"
exit 0
}
#一些警告判斷
warning_if(){
git_https_proxy=$(git config --global https.proxy)
git_http_proxy=$(git config --global http.proxy)
if [[ -z "$git_https_proxy" && -z "$git_http_proxy" ]]; then
echo "未發(fā)現(xiàn)Git代理(屬于正常狀態(tài))"
else
echo "${tty_yellow}
提示:發(fā)現(xiàn)你電腦設置了Git代理赖歌,如果Git報錯,請運行下面兩句話:
git config --global --unset https.proxy
git config --global --unset http.proxy${tty_reset}
"
fi
}
echo "
${tty_green} 開始執(zhí)行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回車。
源有時候不穩(wěn)定坎穿,如果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_NUM in
"2")
echo "
你選擇了清華大學下載源
"
if [[ -z "${HOMEBREW_ON_LINUX-}" ]]; then
USER_HOMEBREW_BOTTLE_DOMAIN=https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles/bottles
else
USER_HOMEBREW_BOTTLE_DOMAIN=https://mirrors.tuna.tsinghua.edu.cn/linuxbrew-bottles/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.bfsu.edu.cn/homebrew-bottles/bottles
else
USER_HOMEBREW_BOTTLE_DOMAIN=https://mirrors.bfsu.edu.cn/linuxbrew-bottles/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/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 "
你選擇了阿里巴巴下載源(有可能維護中,無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/bottles
else
USER_HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/linuxbrew-bottles/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}!H帧苍鲜!此腳本將要刪除之前的brew(包括它下載的軟件),請自行備份玷犹。
->是否現(xiàn)在開始執(zhí)行腳本(N/Y) "
read MY_Del_Old
echo "${tty_reset}"
case $MY_Del_Old in
"y")
echo "--> 腳本開始執(zhí)行"
;;
"Y")
echo "--> 腳本開始執(zhí)行"
;;
*)
echo "你輸入了 $MY_Del_Old 混滔,自行備份老版brew和它下載的軟件, 如果繼續(xù)運行腳本應該輸入Y或者y
"
exit 0
;;
esac
if [[ -z "${HOMEBREW_ON_LINUX-}" ]]; then
#MAC
echo "${tty_yellow} Mac os設置開機密碼方法:
(設置開機密碼:在左上角蘋果圖標->系統(tǒng)偏好設置->"用戶與群組"->更改密碼)
(如果提示This incident will be reported. 在"用戶與群組"中查看是否管理員) ${tty_reset}"
fi
echo "==> 通過命令刪除之前的brew、創(chuàng)建一個新的Homebrew文件夾
${tty_cyan}請輸入開機密碼,輸入過程不顯示坯屿,輸入完后回車${tty_reset}"
sudo echo '開始執(zhí)行'
#刪除以前的Homebrew
RmCreate ${HOMEBREW_REPOSITORY}
RmAndCopy $HOMEBREW_CACHE
RmAndCopy $HOMEBREW_LOGS
# 讓環(huán)境暫時純粹油湖,腳本運行結(jié)束后恢復
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
sudo rm -rf "/Library/Developer/CommandLineTools/"
echo "${tty_cyan}安裝Git${tty_reset}后再運行此腳本,${tty_red}在系統(tǒng)彈窗中點擊“安裝”按鈕
如果沒有彈窗的老系統(tǒng)领跛,需要自己下載安裝:https://sourceforge.net/projects/git-osx-installer/ ${tty_reset}"
xcode-select --install
exit 0
else
echo "${tty_red} 發(fā)現(xiàn)缺少git肺魁,開始安裝,請輸入Y ${tty_reset}"
sudo apt install git
fi
fi
echo "
${tty_cyan}下載速度覺得慢可以ctrl+c或control+c重新運行腳本選擇下載源${tty_reset}
==> 克隆Homebrew基本文件
"
warning_if
sudo git clone ${GIT_SPEED} $USER_BREW_GIT ${HOMEBREW_REPOSITORY}
JudgeSuccess 嘗試再次運行自動腳本選擇其他下載源或者切換網(wǎng)絡 out
#依賴目錄創(chuàng)建 授權(quán)等等
CreateBrewLinkFolder
echo '==> 創(chuàng)建brew的替身'
if [[ "${HOMEBREW_REPOSITORY}" != "${HOMEBREW_PREFIX}" ]]; then
find ${HOMEBREW_PREFIX}/bin -name brew -exec sudo rm -f {} \;
execute "ln" "-sf" "${HOMEBREW_REPOSITORY}/bin/brew" "${HOMEBREW_PREFIX}/bin/brew"
fi
echo "==> 克隆Homebrew Core
${tty_cyan}此處如果顯示Password表示需要再次輸入開機密碼隔节,輸入完后回車${tty_reset}"
sudo mkdir -p ${HOMEBREW_REPOSITORY}/Library/Taps/homebrew/homebrew-core
sudo git clone ${GIT_SPEED} $USER_CORE_GIT ${HOMEBREW_REPOSITORY}/Library/Taps/homebrew/homebrew-core/
JudgeSuccess 嘗試再次運行自動腳本選擇其他下載源或者切換網(wǎng)絡 out
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
sudo mkdir -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
sudo rm -rf ${HOMEBREW_REPOSITORY}/Library/Taps/homebrew/homebrew-cask
echo "${tty_red}嘗試切換下載源或者切換網(wǎng)絡,不過Cask組件非必須模塊≡踅耄可以忽略${tty_reset}"
else
echo "${tty_green}此步驟成功${tty_reset}"
fi
fi
echo "==> 克隆Homebrew services 管理服務的啟停
"
sudo mkdir -p ${HOMEBREW_REPOSITORY}/Library/Taps/homebrew/homebrew-cask
sudo git clone ${GIT_SPEED} $USER_SERVICES_GIT ${HOMEBREW_REPOSITORY}/Library/Taps/homebrew/homebrew-services/
JudgeSuccess
else
#Linux
echo "${tty_yellow} Linux 不支持Cask圖形化軟件下載 此步驟跳過${tty_reset}"
fi
echo '==> 配置國內(nèi)鏡像源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
#刪除之前的環(huán)境變量
if [[ -z "${HOMEBREW_ON_LINUX-}" ]]; then
#Mac
sed -i "" "/ckbrew/d" ${shell_profile}
else
#Linux
sed -i "/ckbrew/d" ${shell_profile}
fi
#寫入環(huán)境變量到文件
echo "環(huán)境變量寫入->${shell_profile}"
echo "
export HOMEBREW_BOTTLE_DOMAIN=${USER_HOMEBREW_BOTTLE_DOMAIN} #ckbrew
eval \$(${HOMEBREW_REPOSITORY}/bin/brew shellenv) #ckbrew
" >> ${shell_profile}
JudgeSuccess
source "${shell_profile}"
if [ $? -ne 0 ];then
echo "${tty_red}發(fā)現(xiàn)錯誤瘾晃,${shell_profile} 文件中有錯誤,建議根據(jù)上一句提示修改幻妓;
否則會導致提示 permission denied: brew${tty_reset}"
fi
AddPermission ${HOMEBREW_REPOSITORY}
if [[ -n "${HOMEBREW_ON_LINUX-}" ]]; then
#檢測linux curl是否有安裝
echo "${tty_red}-檢測curl是否安裝 留意是否需要輸入Y${tty_reset}"
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
echo '
==> 安裝完成蹦误,brew版本
'
brew -v
if [ $? -ne 0 ];then
echo '發(fā)現(xiàn)錯誤,自動修復一次肉津!'
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
#brew 3.1.2版本 修改了很多地址强胰,都寫死在了代碼中,沒有調(diào)用環(huán)境變量妹沙。偶洋。額。距糖。
#ruby下載需要改官方文件
ruby_URL_file=$HOMEBREW_REPOSITORY/Library/Homebrew/cmd/vendor-install.sh
#判斷Mac系統(tǒng)版本
if [[ -z "${HOMEBREW_ON_LINUX-}" ]]; then
if version_gt "$macos_version" "10.14"; then
echo "電腦系統(tǒng)版本:$macos_version"
else
echo "${tty_red}檢測到你不是最新系統(tǒng)玄窝,會有一些報錯,請稍等Ruby下載安裝;${tty_reset}
"
fi
if [[ -f ${ruby_URL_file} ]]; then
sed -i "" "s/ruby_URL=/ruby_URL=\"https:\/\/mirrors.tuna.tsinghua.edu.cn\/homebrew-bottles\/bottles-portable-ruby\/\$ruby_FILENAME\" \#/g" $ruby_URL_file
fi
else
if [[ -f ${ruby_URL_file} ]]; then
sed -i "s/ruby_URL=/ruby_URL=\"https:\/\/mirrors.tuna.tsinghua.edu.cn\/linuxbrew-bottles\/bottles-portable-ruby\/\$ruby_FILENAME\" \#/g" $ruby_URL_file
fi
fi
brew services cleanup
if [[ $GIT_SPEED == "" ]];then
echo '
==> brew update-reset
'
brew update-reset
if [[ $? -ne 0 ]];then
brew config
error_game_over
exit 0
fi
else
#極速模式提示Update修復方法
echo "
${tty_red} 極速版本安裝完成悍引,${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
brew update-reset
"
fi
echo "
${tty_green}Brew自動安裝程序運行完成${tty_reset}
${tty_green}國內(nèi)地址已經(jīng)配置完成${tty_reset}
桌面的Old_Homebrew文件夾,大致看看沒有你需要的可以刪除趣斤。
初步介紹幾個brew命令
本地軟件庫列表:brew ls
查找軟件:brew search google(其中google替換為要查找的關鍵字)
查看brew版本:brew -v 更新brew版本:brew update
安裝cask軟件:brew install --cask firefox 把firefox換成你要安裝的
${tty_green}
歡迎右鍵點擊下方地址-打開URL 來給點個贊${tty_reset}
${tty_underline} https://zhuanlan.zhihu.com/p/111014448 ${tty_reset}
"
if [[ -z "${HOMEBREW_ON_LINUX-}" ]]; then
#Mac
echo "${tty_red} 重啟終端 或者 運行${tty_bold} source ${shell_profile} ${tty_reset} ${tty_red}否則可能無法使用${tty_reset}
"
else
#Linux
echo "${tty_red} Linux需要重啟電腦 或者暫時運行${tty_bold} source ${shell_profile} ${tty_reset} ${tty_red}否則可能無法使用${tty_reset}
"
fi
3. 結(jié)束語俩块;
一個字:香~
順便祝各位中秋節(jié)快樂!
十五的月亮十六圓(LP生日快樂)~
如果本文對您有幫助浓领,麻煩動動手指點點贊玉凯?
謝謝!