当前位置:   article > 正文

mac m1 安装brew_old_homebrew

old_homebrew

目录

0. git 地址(建议)

0.1 卸载brew

0.2 新安装的软件位置

1. 创建shell文件,将下面的内容添加到文件中进行保存

2. 修改文件权限 

3. 执行脚本,按照脚本的提示进行交互安装

 4. 脚本内容


0. git 地址(建议)

HomebrewCN: Homebrew 国内安装脚本

0.1 卸载brew

卸载之后,mac 会在桌面产生一个 old_Homebrew 文件夹,里面包含之前使用 brew 安装过的软件

0.2 新安装的软件位置

安装位置

/opt/homebrew/Cellar

快捷方式位置

/opt/homebrew/bin

1. 创建shell文件,将下面的内容添加到文件中进行保存

vim brewInstall.sh

2. 修改文件权限 

chmod 744 brewInstall.sh

3. 执行脚本,按照脚本的提示进行交互安装

./brewInstall.sh

 4. 脚本内容

  1. #HomeBrew自动安装脚本
  2. #cunkai.wang@foxmail.com
  3. #获取硬件信息
  4. UNAME_MACHINE="$(uname -m)"
  5. #在X86电脑上测试arm电脑
  6. # UNAME_MACHINE="arm64"
  7. # 判断是Linux还是Mac os
  8. OS="$(uname)"
  9. if [[ "$OS" == "Linux" ]]; then
  10. HOMEBREW_ON_LINUX=1
  11. elif [[ "$OS" != "Darwin" ]]; then
  12. echo "Homebrew 只运行在 Mac OS 或 Linux."
  13. fi
  14. # 字符串染色程序
  15. if [[ -t 1 ]]; then
  16. tty_escape() { printf "\033[%sm" "$1"; }
  17. else
  18. tty_escape() { :; }
  19. fi
  20. tty_universal() { tty_escape "0;$1"; } #正常显示
  21. tty_mkbold() { tty_escape "1;$1"; } #设置高亮
  22. tty_underline="$(tty_escape "4;39")" #下划线
  23. tty_blue="$(tty_universal 34)" #蓝色
  24. tty_red="$(tty_universal 31)" #红色
  25. tty_green="$(tty_universal 32)" #绿色
  26. tty_yellow="$(tty_universal 33)" #黄色
  27. tty_bold="$(tty_universal 39)" #加黑
  28. tty_cyan="$(tty_universal 36)" #青色
  29. tty_reset="$(tty_escape 0)" #去除颜色
  30. #用户输入极速安装speed,git克隆只取最近新版本
  31. #但是update会出错,提示需要下载全部数据
  32. GIT_SPEED=""
  33. if [[ $0 == "speed" ]]; then
  34. GIT_SPEED="--depth=1"
  35. else
  36. for dir in $@; do
  37. echo $dir
  38. if [[ $dir == "speed" ]]; then
  39. GIT_SPEED="--depth=1"
  40. fi
  41. done
  42. fi
  43. if [[ $GIT_SPEED != "" ]]; then
  44. echo "${tty_red}
  45. 检测到参数speed,只拉取最新数据,可以正常install使用!
  46. 但是以后brew update的时候会报错,运行报错提示的两句命令即可修复
  47. ${tty_reset}"
  48. fi
  49. #获取前面两个.的数据
  50. major_minor() {
  51. echo "${1%%.*}.$(x="${1#*.}"; echo "${x%%.*}")"
  52. }
  53. #设置一些平台地址
  54. if [[ -z "${HOMEBREW_ON_LINUX-}" ]]; then
  55. #Mac
  56. if [[ "$UNAME_MACHINE" == "arm64" ]]; then
  57. #M1
  58. HOMEBREW_PREFIX="/opt/homebrew"
  59. HOMEBREW_REPOSITORY="${HOMEBREW_PREFIX}"
  60. else
  61. #Inter
  62. HOMEBREW_PREFIX="/usr/local"
  63. HOMEBREW_REPOSITORY="${HOMEBREW_PREFIX}/Homebrew"
  64. fi
  65. HOMEBREW_CACHE="${HOME}/Library/Caches/Homebrew"
  66. HOMEBREW_LOGS="${HOME}/Library/Logs/Homebrew"
  67. #国内没有homebrew-services,手动在gitee创建了一个,有少数人用到。
  68. USER_SERVICES_GIT=https://gitee.com/cunkai/homebrew-services.git
  69. STAT="stat -f"
  70. CHOWN="/usr/sbin/chown"
  71. CHGRP="/usr/bin/chgrp"
  72. GROUP="admin"
  73. TOUCH="/usr/bin/touch"
  74. #获取Mac系统版本
  75. macos_version="$(major_minor "$(/usr/bin/sw_vers -productVersion)")"
  76. else
  77. #Linux
  78. UNAME_MACHINE="$(uname -m)"
  79. HOMEBREW_PREFIX="/home/linuxbrew/.linuxbrew"
  80. HOMEBREW_REPOSITORY="${HOMEBREW_PREFIX}/Homebrew"
  81. HOMEBREW_CACHE="${HOME}/.cache/Homebrew"
  82. HOMEBREW_LOGS="${HOME}/.logs/Homebrew"
  83. STAT="stat --printf"
  84. CHOWN="/bin/chown"
  85. CHGRP="/bin/chgrp"
  86. GROUP="$(id -gn)"
  87. TOUCH="/bin/touch"
  88. fi
  89. #获取系统时间
  90. TIME=$(date "+%Y-%m-%d %H:%M:%S")
  91. JudgeSuccess()
  92. {
  93. if [ $? -ne 0 ];then
  94. echo "${tty_red}m此步骤失败 '$1'${tty_reset}"
  95. if [[ "$2" == 'out' ]]; then
  96. exit 0
  97. fi
  98. else
  99. echo "${tty_green}此步骤成功${tty_reset}"
  100. fi
  101. }
  102. # 判断是否有系统权限
  103. have_sudo_access() {
  104. if [[ -z "${HAVE_SUDO_ACCESS-}" ]]; then
  105. /usr/bin/sudo -l mkdir &>/dev/null
  106. HAVE_SUDO_ACCESS="$?"
  107. fi
  108. if [[ "$HAVE_SUDO_ACCESS" -ne 0 ]]; then
  109. echo "${tty_red}开机密码输入错误,获取权限失败!${tty_reset}"
  110. fi
  111. return "$HAVE_SUDO_ACCESS"
  112. }
  113. abort() {
  114. printf "%s\n" "$1"
  115. # exit 1
  116. }
  117. shell_join() {
  118. local arg
  119. printf "%s" "$1"
  120. shift
  121. for arg in "$@"; do
  122. printf " "
  123. printf "%s" "${arg// /\ }"
  124. done
  125. }
  126. execute() {
  127. if ! "$@"; then
  128. abort "$(printf "${tty_red}此命令运行失败: %s${tty_reset}" "$(shell_join "$@")")"
  129. fi
  130. }
  131. ohai() {
  132. printf "${tty_blue}运行代码 ==>${tty_bold} %s${tty_reset}\n" "$(shell_join "$@")"
  133. }
  134. # 管理员运行
  135. execute_sudo()
  136. {
  137. local -a args=("$@")
  138. if have_sudo_access; then
  139. if [[ -n "${SUDO_ASKPASS-}" ]]; then
  140. args=("-A" "${args[@]}")
  141. fi
  142. ohai "/usr/bin/sudo" "${args[@]}"
  143. execute "/usr/bin/sudo" "${args[@]}"
  144. else
  145. ohai "${args[@]}"
  146. execute "${args[@]}"
  147. fi
  148. }
  149. #添加文件夹权限
  150. AddPermission()
  151. {
  152. execute_sudo "/bin/chmod" "-R" "a+rwx" "$1"
  153. execute_sudo "$CHOWN" "$USER" "$1"
  154. execute_sudo "$CHGRP" "$GROUP" "$1"
  155. }
  156. #创建文件夹
  157. CreateFolder()
  158. {
  159. echo '-> 创建文件夹' $1
  160. execute_sudo "/bin/mkdir" "-p" "$1"
  161. JudgeSuccess
  162. AddPermission $1
  163. }
  164. RmAndCopy()
  165. {
  166. if [[ -d $1 ]]; then
  167. echo " ---备份要删除的$1到系统桌面...."
  168. if ! [[ -d $HOME/Desktop/Old_Homebrew/$TIME/$1 ]]; then
  169. sudo mkdir -p "$HOME/Desktop/Old_Homebrew/$TIME/$1"
  170. fi
  171. sudo cp -rf $1 "$HOME/Desktop/Old_Homebrew/$TIME/$1"
  172. echo " ---$1 备份完成"
  173. fi
  174. sudo rm -rf $1
  175. }
  176. RmCreate()
  177. {
  178. RmAndCopy $1
  179. CreateFolder $1
  180. }
  181. #判断文件夹存在但不可写
  182. exists_but_not_writable() {
  183. [[ -e "$1" ]] && ! [[ -r "$1" && -w "$1" && -x "$1" ]]
  184. }
  185. #文件所有者
  186. get_owner() {
  187. $(shell_join "$STAT %u $1" )
  188. }
  189. #文件本人无权限
  190. file_not_owned() {
  191. [[ "$(get_owner "$1")" != "$(id -u)" ]]
  192. }
  193. #获取所属的组
  194. get_group() {
  195. $(shell_join "$STAT %g $1" )
  196. }
  197. #不在所属组
  198. file_not_grpowned() {
  199. [[ " $(id -G "$USER") " != *" $(get_group "$1") "* ]]
  200. }
  201. #获得当前文件夹权限 例如777
  202. get_permission() {
  203. $(shell_join "$STAT %A $1" )
  204. }
  205. #授权当前用户权限
  206. user_only_chmod() {
  207. [[ -d "$1" ]] && [[ "$(get_permission "$1")" != "755" ]]
  208. }
  209. #创建brew需要的目录 直接复制于国外版本,同步
  210. CreateBrewLinkFolder()
  211. {
  212. echo "--创建Brew所需要的目录"
  213. directories=(bin etc include lib sbin share opt var
  214. Frameworks
  215. etc/bash_completion.d lib/pkgconfig
  216. share/aclocal share/doc share/info share/locale share/man
  217. share/man/man1 share/man/man2 share/man/man3 share/man/man4
  218. share/man/man5 share/man/man6 share/man/man7 share/man/man8
  219. var/log var/homebrew var/homebrew/linked
  220. bin/brew)
  221. group_chmods=()
  222. for dir in "${directories[@]}"; do
  223. if exists_but_not_writable "${HOMEBREW_PREFIX}/${dir}"; then
  224. group_chmods+=("${HOMEBREW_PREFIX}/${dir}")
  225. fi
  226. done
  227. directories=(share/zsh share/zsh/site-functions)
  228. zsh_dirs=()
  229. for dir in "${directories[@]}"; do
  230. zsh_dirs+=("${HOMEBREW_PREFIX}/${dir}")
  231. done
  232. directories=(bin etc include lib sbin share var opt
  233. share/zsh share/zsh/site-functions
  234. var/homebrew var/homebrew/linked
  235. Cellar Caskroom Frameworks)
  236. mkdirs=()
  237. for dir in "${directories[@]}"; do
  238. if ! [[ -d "${HOMEBREW_PREFIX}/${dir}" ]]; then
  239. mkdirs+=("${HOMEBREW_PREFIX}/${dir}")
  240. fi
  241. done
  242. user_chmods=()
  243. if [[ "${#zsh_dirs[@]}" -gt 0 ]]; then
  244. for dir in "${zsh_dirs[@]}"; do
  245. if user_only_chmod "${dir}"; then
  246. user_chmods+=("${dir}")
  247. fi
  248. done
  249. fi
  250. chmods=()
  251. if [[ "${#group_chmods[@]}" -gt 0 ]]; then
  252. chmods+=("${group_chmods[@]}")
  253. fi
  254. if [[ "${#user_chmods[@]}" -gt 0 ]]; then
  255. chmods+=("${user_chmods[@]}")
  256. fi
  257. chowns=()
  258. chgrps=()
  259. if [[ "${#chmods[@]}" -gt 0 ]]; then
  260. for dir in "${chmods[@]}"; do
  261. if file_not_owned "${dir}"; then
  262. chowns+=("${dir}")
  263. fi
  264. if file_not_grpowned "${dir}"; then
  265. chgrps+=("${dir}")
  266. fi
  267. done
  268. fi
  269. if [[ -d "${HOMEBREW_PREFIX}" ]]; then
  270. if [[ "${#chmods[@]}" -gt 0 ]]; then
  271. execute_sudo "/bin/chmod" "u+rwx" "${chmods[@]}"
  272. fi
  273. if [[ "${#group_chmods[@]}" -gt 0 ]]; then
  274. execute_sudo "/bin/chmod" "g+rwx" "${group_chmods[@]}"
  275. fi
  276. if [[ "${#user_chmods[@]}" -gt 0 ]]; then
  277. execute_sudo "/bin/chmod" "755" "${user_chmods[@]}"
  278. fi
  279. if [[ "${#chowns[@]}" -gt 0 ]]; then
  280. execute_sudo "$CHOWN" "$USER" "${chowns[@]}"
  281. fi
  282. if [[ "${#chgrps[@]}" -gt 0 ]]; then
  283. execute_sudo "$CHGRP" "$GROUP" "${chgrps[@]}"
  284. fi
  285. else
  286. execute_sudo "/bin/mkdir" "-p" "${HOMEBREW_PREFIX}"
  287. if [[ -z "${HOMEBREW_ON_LINUX-}" ]]; then
  288. execute_sudo "$CHOWN" "root:wheel" "${HOMEBREW_PREFIX}"
  289. else
  290. execute_sudo "$CHOWN" "$USER:$GROUP" "${HOMEBREW_PREFIX}"
  291. fi
  292. fi
  293. if [[ "${#mkdirs[@]}" -gt 0 ]]; then
  294. execute_sudo "/bin/mkdir" "-p" "${mkdirs[@]}"
  295. execute_sudo "/bin/chmod" "g+rwx" "${mkdirs[@]}"
  296. execute_sudo "$CHOWN" "$USER" "${mkdirs[@]}"
  297. execute_sudo "$CHGRP" "$GROUP" "${mkdirs[@]}"
  298. fi
  299. if ! [[ -d "${HOMEBREW_REPOSITORY}" ]]; then
  300. execute_sudo "/bin/mkdir" "-p" "${HOMEBREW_REPOSITORY}"
  301. fi
  302. execute_sudo "$CHOWN" "-R" "$USER:$GROUP" "${HOMEBREW_REPOSITORY}"
  303. if ! [[ -d "${HOMEBREW_CACHE}" ]]; then
  304. if [[ -z "${HOMEBREW_ON_LINUX-}" ]]; then
  305. execute_sudo "/bin/mkdir" "-p" "${HOMEBREW_CACHE}"
  306. else
  307. execute "/bin/mkdir" "-p" "${HOMEBREW_CACHE}"
  308. fi
  309. fi
  310. if exists_but_not_writable "${HOMEBREW_CACHE}"; then
  311. execute_sudo "/bin/chmod" "g+rwx" "${HOMEBREW_CACHE}"
  312. fi
  313. if file_not_owned "${HOMEBREW_CACHE}"; then
  314. execute_sudo "$CHOWN" "-R" "$USER" "${HOMEBREW_CACHE}"
  315. fi
  316. if file_not_grpowned "${HOMEBREW_CACHE}"; then
  317. execute_sudo "$CHGRP" "-R" "$GROUP" "${HOMEBREW_CACHE}"
  318. fi
  319. if [[ -d "${HOMEBREW_CACHE}" ]]; then
  320. execute "$TOUCH" "${HOMEBREW_CACHE}/.cleaned"
  321. fi
  322. echo "--依赖目录脚本运行完成"
  323. }
  324. #git提交
  325. git_commit(){
  326. git add .
  327. git commit -m "your del"
  328. }
  329. #version_gt 判断$1是否大于$2
  330. version_gt() {
  331. [[ "${1%.*}" -gt "${2%.*}" ]] || [[ "${1%.*}" -eq "${2%.*}" && "${1#*.}" -gt "${2#*.}" ]]
  332. }
  333. #version_ge 判断$1是否大于等于$2
  334. version_ge() {
  335. [[ "${1%.*}" -gt "${2%.*}" ]] || [[ "${1%.*}" -eq "${2%.*}" && "${1#*.}" -ge "${2#*.}" ]]
  336. }
  337. #version_lt 判断$1是否小于$2
  338. version_lt() {
  339. [[ "${1%.*}" -lt "${2%.*}" ]] || [[ "${1%.*}" -eq "${2%.*}" && "${1#*.}" -lt "${2#*.}" ]]
  340. }
  341. #发现错误 关闭脚本 提示如何解决
  342. error_game_over(){
  343. echo "
  344. ${tty_red}失败$MY_DOWN_NUM 去下面文章看一下第二部分的常见错误解决办法
  345. https://zhuanlan.zhihu.com/p/111014448
  346. 如果没有解决,把运行脚本过程截图发到 cunkai.wang@foxmail.com ${tty_reset}
  347. "
  348. exit 0
  349. }
  350. #一些警告判断
  351. warning_if(){
  352. git_https_proxy=$(git config --global https.proxy)
  353. git_http_proxy=$(git config --global http.proxy)
  354. if [[ -z "$git_https_proxy" && -z "$git_http_proxy" ]]; then
  355. echo "未发现Git代理(属于正常状态)"
  356. else
  357. echo "${tty_yellow}
  358. 提示:发现你电脑设置了Git代理,如果Git报错,请运行下面两句话:
  359. git config --global --unset https.proxy
  360. git config --global --unset http.proxy${tty_reset}
  361. "
  362. fi
  363. }
  364. echo "
  365. ${tty_green} 开始执行Brew自动安装程序 ${tty_reset}
  366. ${tty_cyan} [cunkai.wang@foxmail.com] ${tty_reset}
  367. ['$TIME']['$macos_version']
  368. ${tty_cyan} https://zhuanlan.zhihu.com/p/111014448 ${tty_reset}
  369. "
  370. #选择一个下载源
  371. echo -n "${tty_green}
  372. 请选择一个下载镜像,例如中科大,输入1回车。
  373. 源有时候不稳定,如果git克隆报错重新运行脚本选择源。cask非必须,有部分人需要。
  374. 1、中科大下载源 2、清华大学下载源(推荐) 3、北京外国语大学下载源 ${tty_reset}"
  375. if [[ -z "${HOMEBREW_ON_LINUX-}" ]]; then
  376. #mac才显示腾讯 阿里,他们对linux目前支持很差
  377. echo "${tty_green} 4、腾讯下载源(不推荐) 5、阿里巴巴下载源(不推荐 缺少cask源) ${tty_reset} "
  378. fi
  379. echo -n "
  380. ${tty_blue}请输入序号: "
  381. read MY_DOWN_NUM
  382. echo "${tty_reset}"
  383. case $MY_DOWN_NUM in
  384. "2")
  385. echo "
  386. 你选择了清华大学下载源
  387. "
  388. if [[ -z "${HOMEBREW_ON_LINUX-}" ]]; then
  389. USER_HOMEBREW_BOTTLE_DOMAIN=https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles/bottles
  390. else
  391. USER_HOMEBREW_BOTTLE_DOMAIN=https://mirrors.tuna.tsinghua.edu.cn/linuxbrew-bottles/bottles
  392. fi
  393. #HomeBrew基础框架
  394. USER_BREW_GIT=https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git
  395. #HomeBrew Core
  396. if [[ -z "${HOMEBREW_ON_LINUX-}" ]]; then
  397. USER_CORE_GIT=https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git
  398. else
  399. USER_CORE_GIT=https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/linuxbrew-core.git
  400. fi
  401. #HomeBrew Cask
  402. USER_CASK_GIT=https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-cask.git
  403. USER_CASK_FONTS_GIT=https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-cask-fonts.git
  404. USER_CASK_DRIVERS_GIT=https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-cask-drivers.git
  405. ;;
  406. "3")
  407. echo "
  408. 北京外国语大学下载源
  409. "
  410. if [[ -z "${HOMEBREW_ON_LINUX-}" ]]; then
  411. USER_HOMEBREW_BOTTLE_DOMAIN=https://mirrors.bfsu.edu.cn/homebrew-bottles/bottles
  412. else
  413. USER_HOMEBREW_BOTTLE_DOMAIN=https://mirrors.bfsu.edu.cn/linuxbrew-bottles/bottles
  414. fi
  415. #HomeBrew基础框架
  416. USER_BREW_GIT=https://mirrors.bfsu.edu.cn/git/homebrew/brew.git
  417. #HomeBrew Core
  418. if [[ -z "${HOMEBREW_ON_LINUX-}" ]]; then
  419. USER_CORE_GIT=https://mirrors.bfsu.edu.cn/git/homebrew/homebrew-core.git
  420. else
  421. USER_CORE_GIT=https://mirrors.bfsu.edu.cn/git/homebrew/linuxbrew-core.git
  422. fi
  423. #HomeBrew Cask
  424. USER_CASK_GIT=https://mirrors.bfsu.edu.cn/git/homebrew/homebrew-cask.git
  425. USER_CASK_FONTS_GIT=https://mirrors.bfsu.edu.cn/git/homebrew/homebrew-cask-fonts.git
  426. USER_CASK_DRIVERS_GIT=https://mirrors.bfsu.edu.cn/git/homebrew/homebrew-cask-drivers.git
  427. ;;
  428. "4")
  429. echo "
  430. 你选择了腾讯下载源
  431. "
  432. USER_HOMEBREW_BOTTLE_DOMAIN=https://mirrors.cloud.tencent.com/homebrew-bottles/bottles
  433. #HomeBrew基础框架
  434. USER_BREW_GIT=https://mirrors.cloud.tencent.com/homebrew/brew.git
  435. #HomeBrew Core
  436. if [[ -z "${HOMEBREW_ON_LINUX-}" ]]; then
  437. USER_CORE_GIT=https://mirrors.cloud.tencent.com/homebrew/homebrew-core.git
  438. else
  439. USER_CORE_GIT=https://mirrors.cloud.tencent.com/homebrew/linuxbrew-core.git
  440. fi
  441. #HomeBrew Cask
  442. USER_CASK_GIT=https://mirrors.cloud.tencent.com/homebrew/homebrew-cask.git
  443. ;;
  444. "5")
  445. echo "
  446. 你选择了阿里巴巴下载源(有可能维护中,无Linux版本)
  447. "
  448. USER_HOMEBREW_BOTTLE_DOMAIN=https://mirrors.aliyun.com/homebrew/homebrew-bottles
  449. #HomeBrew基础框架
  450. USER_BREW_GIT=https://mirrors.aliyun.com/homebrew/brew.git
  451. #HomeBrew Core
  452. if [[ -z "${HOMEBREW_ON_LINUX-}" ]]; then
  453. USER_CORE_GIT=https://mirrors.aliyun.com/homebrew/homebrew-core.git
  454. else
  455. USER_CORE_GIT=https://mirrors.ustc.edu.cn/linuxbrew-core.git
  456. echo "阿里巴巴无core,这里替换为了中国科学技术大学的linuxbrew-core"
  457. fi
  458. #HomeBrew Cask
  459. USER_CASK_GIT=https://mirrors.aliyun.com/homebrew/homebrew-cask.git
  460. ;;
  461. *)
  462. echo "
  463. 你选择了中国科学技术大学下载源
  464. "
  465. #HomeBrew 下载源 install
  466. if [[ -z "${HOMEBREW_ON_LINUX-}" ]]; then
  467. USER_HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles/bottles
  468. else
  469. USER_HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/linuxbrew-bottles/bottles
  470. fi
  471. #HomeBrew基础框架
  472. USER_BREW_GIT=https://mirrors.ustc.edu.cn/brew.git
  473. #HomeBrew Core
  474. if [[ -z "${HOMEBREW_ON_LINUX-}" ]]; then
  475. USER_CORE_GIT=https://mirrors.ustc.edu.cn/homebrew-core.git
  476. else
  477. USER_CORE_GIT=https://mirrors.ustc.edu.cn/linuxbrew-core.git
  478. fi
  479. #HomeBrew Cask
  480. USER_CASK_GIT=https://mirrors.ustc.edu.cn/homebrew-cask.git
  481. ;;
  482. esac
  483. echo -n "${tty_green}!!!此脚本将要删除之前的brew(包括它下载的软件),请自行备份。
  484. ->是否现在开始执行脚本(N/Y) "
  485. read MY_Del_Old
  486. echo "${tty_reset}"
  487. case $MY_Del_Old in
  488. "y")
  489. echo "--> 脚本开始执行"
  490. ;;
  491. "Y")
  492. echo "--> 脚本开始执行"
  493. ;;
  494. *)
  495. echo "你输入了 $MY_Del_Old ,自行备份老版brew和它下载的软件, 如果继续运行脚本应该输入Y或者y
  496. "
  497. exit 0
  498. ;;
  499. esac
  500. if [[ -z "${HOMEBREW_ON_LINUX-}" ]]; then
  501. #MAC
  502. echo "${tty_yellow} Mac os设置开机密码方法:
  503. (设置开机密码:在左上角苹果图标->系统偏好设置->"用户与群组"->更改密码)
  504. (如果提示This incident will be reported. 在"用户与群组"中查看是否管理员) ${tty_reset}"
  505. fi
  506. echo "==> 通过命令删除之前的brew、创建一个新的Homebrew文件夹
  507. ${tty_cyan}请输入开机密码,输入过程不显示,输入完后回车${tty_reset}"
  508. sudo echo '开始执行'
  509. #删除以前的Homebrew
  510. RmCreate ${HOMEBREW_REPOSITORY}
  511. RmAndCopy $HOMEBREW_CACHE
  512. RmAndCopy $HOMEBREW_LOGS
  513. # 让环境暂时纯粹,脚本运行结束后恢复
  514. if [[ -z "${HOMEBREW_ON_LINUX-}" ]]; then
  515. export PATH=/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:${HOMEBREW_REPOSITORY}/bin
  516. fi
  517. git --version
  518. if [ $? -ne 0 ];then
  519. if [[ -z "${HOMEBREW_ON_LINUX-}" ]]; then
  520. sudo rm -rf "/Library/Developer/CommandLineTools/"
  521. echo "${tty_cyan}安装Git${tty_reset}后再运行此脚本,${tty_red}在系统弹窗中点击“安装”按钮
  522. 如果没有弹窗的老系统,需要自己下载安装:https://sourceforge.net/projects/git-osx-installer/ ${tty_reset}"
  523. xcode-select --install
  524. exit 0
  525. else
  526. echo "${tty_red} 发现缺少git,开始安装,请输入Y ${tty_reset}"
  527. sudo apt install git
  528. fi
  529. fi
  530. echo "
  531. ${tty_cyan}下载速度觉得慢可以ctrl+c或control+c重新运行脚本选择下载源${tty_reset}
  532. ==> 克隆Homebrew基本文件
  533. "
  534. warning_if
  535. sudo git clone ${GIT_SPEED} $USER_BREW_GIT ${HOMEBREW_REPOSITORY}
  536. JudgeSuccess 尝试再次运行自动脚本选择其他下载源或者切换网络 out
  537. #依赖目录创建 授权等等
  538. CreateBrewLinkFolder
  539. echo '==> 创建brew的替身'
  540. if [[ "${HOMEBREW_REPOSITORY}" != "${HOMEBREW_PREFIX}" ]]; then
  541. find ${HOMEBREW_PREFIX}/bin -name brew -exec sudo rm -f {} \;
  542. execute "ln" "-sf" "${HOMEBREW_REPOSITORY}/bin/brew" "${HOMEBREW_PREFIX}/bin/brew"
  543. fi
  544. echo "==> 克隆Homebrew Core
  545. ${tty_cyan}此处如果显示Password表示需要再次输入开机密码,输入完后回车${tty_reset}"
  546. sudo mkdir -p ${HOMEBREW_REPOSITORY}/Library/Taps/homebrew/homebrew-core
  547. sudo git clone ${GIT_SPEED} $USER_CORE_GIT ${HOMEBREW_REPOSITORY}/Library/Taps/homebrew/homebrew-core/
  548. JudgeSuccess 尝试再次运行自动脚本选择其他下载源或者切换网络 out
  549. if [[ -z "${HOMEBREW_ON_LINUX-}" ]]; then
  550. #MAC
  551. echo "==> 克隆Homebrew Cask 图形化软件
  552. ${tty_cyan}此处如果显示Password表示需要再次输入开机密码,输入完后回车${tty_reset}"
  553. if [[ "$MY_DOWN_NUM" -eq "5" ]];then
  554. echo "${tty_yellow} 阿里源没有Cask 跳过${tty_reset}"
  555. else
  556. sudo mkdir -p ${HOMEBREW_REPOSITORY}/Library/Taps/homebrew/homebrew-cask
  557. sudo git clone ${GIT_SPEED} $USER_CASK_GIT ${HOMEBREW_REPOSITORY}/Library/Taps/homebrew/homebrew-cask/
  558. if [ $? -ne 0 ];then
  559. sudo rm -rf ${HOMEBREW_REPOSITORY}/Library/Taps/homebrew/homebrew-cask
  560. echo "${tty_red}尝试切换下载源或者切换网络,不过Cask组件非必须模块。可以忽略${tty_reset}"
  561. else
  562. echo "${tty_green}此步骤成功${tty_reset}"
  563. fi
  564. fi
  565. echo "==> 克隆Homebrew services 管理服务的启停
  566. "
  567. sudo mkdir -p ${HOMEBREW_REPOSITORY}/Library/Taps/homebrew/homebrew-cask
  568. sudo git clone ${GIT_SPEED} $USER_SERVICES_GIT ${HOMEBREW_REPOSITORY}/Library/Taps/homebrew/homebrew-services/
  569. JudgeSuccess
  570. else
  571. #Linux
  572. echo "${tty_yellow} Linux 不支持Cask图形化软件下载 此步骤跳过${tty_reset}"
  573. fi
  574. echo '==> 配置国内镜像源HOMEBREW BOTTLE'
  575. #判断下终端是Bash还是zsh
  576. case "$SHELL" in
  577. */bash*)
  578. if [[ -r "$HOME/.bash_profile" ]]; then
  579. shell_profile="${HOME}/.bash_profile"
  580. else
  581. shell_profile="${HOME}/.profile"
  582. fi
  583. ;;
  584. */zsh*)
  585. shell_profile="${HOME}/.zprofile"
  586. ;;
  587. *)
  588. shell_profile="${HOME}/.profile"
  589. ;;
  590. esac
  591. if [[ -f ${shell_profile} ]]; then
  592. AddPermission ${shell_profile}
  593. fi
  594. #删除之前的环境变量
  595. if [[ -z "${HOMEBREW_ON_LINUX-}" ]]; then
  596. #Mac
  597. sed -i "" "/ckbrew/d" ${shell_profile}
  598. else
  599. #Linux
  600. sed -i "/ckbrew/d" ${shell_profile}
  601. fi
  602. #写入环境变量到文件
  603. echo "环境变量写入->${shell_profile}"
  604. echo "
  605. export HOMEBREW_BOTTLE_DOMAIN=${USER_HOMEBREW_BOTTLE_DOMAIN} #ckbrew
  606. eval \$(${HOMEBREW_REPOSITORY}/bin/brew shellenv) #ckbrew
  607. " >> ${shell_profile}
  608. JudgeSuccess
  609. source "${shell_profile}"
  610. if [ $? -ne 0 ];then
  611. echo "${tty_red}发现错误,${shell_profile} 文件中有错误,建议根据上一句提示修改;
  612. 否则会导致提示 permission denied: brew${tty_reset}"
  613. fi
  614. AddPermission ${HOMEBREW_REPOSITORY}
  615. if [[ -n "${HOMEBREW_ON_LINUX-}" ]]; then
  616. #检测linux curl是否有安装
  617. echo "-检测curl是否安装"
  618. curl -V
  619. if [ $? -ne 0 ];then
  620. sudo apt-get install curl
  621. if [ $? -ne 0 ];then
  622. sudo yum install curl
  623. if [ $? -ne 0 ];then
  624. echo '失败 请自行安装curl 可以参考https://www.howtoing.com/install-curl-in-linux'
  625. error_game_over
  626. fi
  627. fi
  628. fi
  629. fi
  630. echo '
  631. ==> 安装完成,brew版本
  632. '
  633. brew -v
  634. if [ $? -ne 0 ];then
  635. echo '发现错误,自动修复一次!'
  636. rm -rf $HOMEBREW_CACHE
  637. export PATH=/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:${HOMEBREW_REPOSITORY}/bin
  638. brew update-reset
  639. brew -v
  640. if [ $? -ne 0 ];then
  641. error_game_over
  642. fi
  643. else
  644. echo "${tty_green}Brew前期配置成功${tty_reset}"
  645. fi
  646. #先暂时设置到清华大学源,中科大没有Ruby下载镜像
  647. HOMEBREW_BOTTLE_DOMAIN=https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles/bottles-portable-ruby/
  648. #判断Mac系统版本
  649. if [[ -z "${HOMEBREW_ON_LINUX-}" ]]; then
  650. if version_gt "$macos_version" "10.14"; then
  651. echo "电脑系统版本:$macos_version"
  652. else
  653. echo "${tty_red}检测到你不是最新系统,会有一些报错,请稍等Ruby下载安装;${tty_reset}
  654. "
  655. fi
  656. fi
  657. brew services cleanup
  658. if [[ $GIT_SPEED == "" ]];then
  659. echo '
  660. ==> brew update
  661. '
  662. brew update
  663. if [[ $? -ne 0 ]];then
  664. error_game_over
  665. exit 0
  666. fi
  667. else
  668. #极速模式提示Update修复方法
  669. echo "
  670. ${tty_red} 极速版本安装完成,${tty_reset} install功能正常,如果需要update功能请自行运行下面两句命令
  671. git -C ${HOMEBREW_REPOSITORY}/Library/Taps/homebrew/homebrew-core fetch --unshallow
  672. git -C ${HOMEBREW_REPOSITORY}/Library/Taps/homebrew/homebrew-cask fetch --unshallow
  673. "
  674. fi
  675. echo "
  676. ${tty_green}Brew自动安装程序运行完成${tty_reset}
  677. ${tty_green}国内地址已经配置完成${tty_reset}
  678. 桌面的Old_Homebrew文件夹,大致看看没有你需要的可以删除。
  679. 初步介绍几个brew命令
  680. 本地软件库列表:brew ls
  681. 查找软件:brew search google(其中google替换为要查找的关键字)
  682. 查看brew版本:brew -v 更新brew版本:brew update
  683. 安装cask软件:brew install --cask firefox 把firefox换成你要安装的
  684. ${tty_green}
  685. 欢迎右键点击下方地址-打开URL 来给点个赞${tty_reset}
  686. ${tty_underline} https://zhuanlan.zhihu.com/p/111014448 ${tty_reset}
  687. "
  688. if [[ -z "${HOMEBREW_ON_LINUX-}" ]]; then
  689. #Mac
  690. echo "${tty_green} 重启终端 或者 运行${tty_bold} source ${shell_profile} ${tty_reset},否则可能无法使用
  691. "
  692. else
  693. #Linux
  694. echo "${tty_green} Linux需要重启电脑 或者暂时运行${tty_bold} source ${shell_profile} ${tty_reset},否则可能无法使用
  695. "
  696. fi

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/256729
推荐阅读
  

闽ICP备14008679号