install.sh 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. #!/bin/sh
  2. #
  3. # This script should be run via curl:
  4. # sh -c "$(curl -fsSL https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"
  5. # or wget:
  6. # sh -c "$(wget -qO- https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"
  7. #
  8. # As an alternative, you can first download the install script and run it afterwards:
  9. # wget https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh
  10. # sh install.sh
  11. #
  12. # Respects these environment variables for tweaking the installation process:
  13. # REPO - name of the GitHub repo to install from (default: robbyrussell/oh-my-zsh)
  14. # REMOTE - full remote URL of the git repo to install (default: GitHub via HTTPS)
  15. # BRANCH - branch to check out immediately after install (default: master)
  16. #
  17. set -e
  18. # Default settings
  19. ZSH=${ZSH:-~/.oh-my-zsh}
  20. REPO=${REPO:-robbyrussell/oh-my-zsh}
  21. REMOTE=${REMOTE:-https://github.com/${REPO}.git}
  22. BRANCH=${BRANCH:-master}
  23. command_exists() {
  24. command -v "$@" >/dev/null 2>&1
  25. }
  26. error() {
  27. echo ${RED}"Error: $@"${RESET} >&2
  28. }
  29. # Set up color sequences
  30. setup_color() {
  31. if command_exists tput; then
  32. ncolors=$(tput colors)
  33. else
  34. ncolors=0
  35. fi
  36. # Only use colors if connected to a terminal that supports them
  37. if [ -t 1 ] && [ $ncolors -ge 8 ]; then
  38. RED="$(tput setaf 1)"
  39. GREEN="$(tput setaf 2)"
  40. YELLOW="$(tput setaf 3)"
  41. BLUE="$(tput setaf 4)"
  42. BOLD="$(tput bold)"
  43. RESET="$(tput sgr0)"
  44. else
  45. RED=$(printf '\033[31m')
  46. GREEN=$(printf '\033[32m')
  47. YELLOW=$(printf '\033[33m')
  48. BLUE=$(printf '\033[34m')
  49. BOLD=$(printf '\033[1m')
  50. RESET=$(printf '\033[m')
  51. fi
  52. }
  53. setup_ohmyzsh() {
  54. # Prevent the cloned repository from having insecure permissions. Failing to do
  55. # so causes compinit() calls to fail with "command not found: compdef" errors
  56. # for users with insecure umasks (e.g., "002", allowing group writability). Note
  57. # that this will be ignored under Cygwin by default, as Windows ACLs take
  58. # precedence over umasks except for filesystems mounted with option "noacl".
  59. umask g-w,o-w
  60. echo "${BLUE}Cloning Oh My Zsh...${RESET}"
  61. command_exists git || {
  62. error "git is not installed"
  63. exit 1
  64. }
  65. if [ "$OSTYPE" = cygwin ] && git --version | grep -q msysgit; then
  66. error "Windows/MSYS Git is not supported on Cygwin"
  67. error "Make sure the Cygwin git package is installed and is first on the \$PATH"
  68. exit 1
  69. fi
  70. git clone --depth=1 --branch "$BRANCH" "$REMOTE" "$ZSH" || {
  71. error "git clone of oh-my-zsh repo failed"
  72. exit 1
  73. }
  74. }
  75. setup_zshrc() {
  76. echo "${BLUE}Looking for an existing zsh config...${RESET}"
  77. if [ -f ~/.zshrc ] || [ -h ~/.zshrc ]; then
  78. echo "${YELLOW}Found ~/.zshrc.${GREEN} Backing up to ~/.zshrc.pre-oh-my-zsh.${RESET}"
  79. mv ~/.zshrc ~/.zshrc.pre-oh-my-zsh
  80. fi
  81. echo "${BLUE}Using the Oh My Zsh template file and adding it to ~/.zshrc.${RESET}"
  82. cp "$ZSH/templates/zshrc.zsh-template" ~/.zshrc
  83. sed "/^export ZSH=/ c\\
  84. export ZSH=\"$ZSH\"
  85. " ~/.zshrc > ~/.zshrc-omztemp
  86. mv -f ~/.zshrc-omztemp ~/.zshrc
  87. }
  88. setup_shell() {
  89. # If this user's login shell is already "zsh", do not attempt to switch.
  90. if [ "$(basename "$SHELL")" = "zsh" ]; then
  91. return
  92. fi
  93. # If this platform doesn't provide a "chsh" command, bail out.
  94. if ! command_exists chsh; then
  95. cat <<-EOF
  96. I can't change your shell automatically because this system does not have chsh.
  97. ${BLUE}Please manually change your default shell to zsh${RESET}
  98. EOF
  99. return
  100. fi
  101. echo "${BLUE}Time to change your default shell to zsh!${RESET}"
  102. # Test for the right location of the "shells" file
  103. if [ -f /etc/shells ]; then
  104. shells_file=/etc/shells
  105. elif [ -f /usr/share/defaults/etc/shells ]; then # Solus OS
  106. shells_file=/usr/share/defaults/etc/shells
  107. else
  108. error "could not find /etc/shells file. Change your default shell manually."
  109. return
  110. fi
  111. # Get the path to the right zsh binary
  112. # 1. Use the most preceding one based on $PATH, then check that it's in the shells file
  113. # 2. If that fails, get a zsh path from the shells file, then check it actually exists
  114. if ! zsh=$(which zsh) || ! grep -qx "$zsh" "$shells_file"; then
  115. if ! zsh=$(grep '^/.*/zsh$' "$shells_file" | tail -1) || [ ! -f "$zsh" ]; then
  116. error "no available zsh binary found. Change your default shell manually."
  117. return
  118. fi
  119. fi
  120. # Actually change the default shell to zsh
  121. if ! chsh -s "$zsh"; then
  122. error "chsh command unsuccessful. Change your default shell manually."
  123. fi
  124. }
  125. main() {
  126. setup_color
  127. if ! command_exists zsh; then
  128. echo "${YELLOW}Zsh is not installed.${RESET} Please install zsh first."
  129. exit 1
  130. fi
  131. if [ -d "$ZSH" ]; then
  132. cat <<-EOF
  133. ${YELLOW}You already have Oh My Zsh installed.${RESET}
  134. You'll need to remove '$ZSH' if you want to reinstall.
  135. EOF
  136. exit 1
  137. fi
  138. setup_ohmyzsh
  139. setup_zshrc
  140. setup_shell
  141. printf "$GREEN"
  142. cat <<-'EOF'
  143. __ __
  144. ____ / /_ ____ ___ __ __ ____ _____/ /_
  145. / __ \/ __ \ / __ `__ \/ / / / /_ / / ___/ __ \
  146. / /_/ / / / / / / / / / / /_/ / / /_(__ ) / / /
  147. \____/_/ /_/ /_/ /_/ /_/\__, / /___/____/_/ /_/
  148. /____/ ....is now installed!
  149. Please look over the ~/.zshrc file to select plugins, themes, and options.
  150. p.s. Follow us on https://twitter.com/ohmyzsh
  151. p.p.s. Get stickers, shirts, and coffee mugs at https://shop.planetargon.com/collections/oh-my-zsh
  152. EOF
  153. printf "$RESET"
  154. exec zsh -l
  155. }
  156. main