install.sh 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. # ZSH - path to the Oh My Zsh repository folder (default: $HOME/.oh-my-zsh)
  14. # REPO - name of the GitHub repo to install from (default: robbyrussell/oh-my-zsh)
  15. # REMOTE - full remote URL of the git repo to install (default: GitHub via HTTPS)
  16. # BRANCH - branch to check out immediately after install (default: master)
  17. # Other options:
  18. # CHSH - set to no tells the installer not to change the default shell (default: yes)
  19. #
  20. set -e
  21. # Default settings
  22. ZSH=${ZSH:-~/.oh-my-zsh}
  23. REPO=${REPO:-robbyrussell/oh-my-zsh}
  24. REMOTE=${REMOTE:-https://github.com/${REPO}.git}
  25. BRANCH=${BRANCH:-master}
  26. # Other options
  27. CHSH=${CHSH:-yes}
  28. command_exists() {
  29. command -v "$@" >/dev/null 2>&1
  30. }
  31. error() {
  32. echo ${RED}"Error: $@"${RESET} >&2
  33. }
  34. # Set up color sequences
  35. setup_color() {
  36. if command_exists tput; then
  37. ncolors=$(tput colors)
  38. else
  39. ncolors=0
  40. fi
  41. # Only use colors if connected to a terminal that supports them
  42. if [ -t 1 ] && [ $ncolors -ge 8 ]; then
  43. RED="$(tput setaf 1)"
  44. GREEN="$(tput setaf 2)"
  45. YELLOW="$(tput setaf 3)"
  46. BLUE="$(tput setaf 4)"
  47. BOLD="$(tput bold)"
  48. RESET="$(tput sgr0)"
  49. else
  50. RED=$(printf '\033[31m')
  51. GREEN=$(printf '\033[32m')
  52. YELLOW=$(printf '\033[33m')
  53. BLUE=$(printf '\033[34m')
  54. BOLD=$(printf '\033[1m')
  55. RESET=$(printf '\033[m')
  56. fi
  57. }
  58. setup_ohmyzsh() {
  59. # Prevent the cloned repository from having insecure permissions. Failing to do
  60. # so causes compinit() calls to fail with "command not found: compdef" errors
  61. # for users with insecure umasks (e.g., "002", allowing group writability). Note
  62. # that this will be ignored under Cygwin by default, as Windows ACLs take
  63. # precedence over umasks except for filesystems mounted with option "noacl".
  64. umask g-w,o-w
  65. echo "${BLUE}Cloning Oh My Zsh...${RESET}"
  66. command_exists git || {
  67. error "git is not installed"
  68. exit 1
  69. }
  70. if [ "$OSTYPE" = cygwin ] && git --version | grep -q msysgit; then
  71. error "Windows/MSYS Git is not supported on Cygwin"
  72. error "Make sure the Cygwin git package is installed and is first on the \$PATH"
  73. exit 1
  74. fi
  75. git clone --depth=1 --branch "$BRANCH" "$REMOTE" "$ZSH" || {
  76. error "git clone of oh-my-zsh repo failed"
  77. exit 1
  78. }
  79. }
  80. setup_zshrc() {
  81. # Keep most recent old .zshrc at .zshrc.pre-oh-my-zsh, and older ones
  82. # with datestamp of installation that moved them aside, so we never actually
  83. # destroy a user's original zshrc
  84. echo "${BLUE}Looking for an existing zsh config...${RESET}"
  85. # Must use this exact name so uninstall.sh can find it
  86. OLD_ZSHRC=~/.zshrc.pre-oh-my-zsh
  87. if [ -f ~/.zshrc ] || [ -h ~/.zshrc ]; then
  88. if [ -e "$OLD_ZSHRC" ]; then
  89. OLD_OLD_ZSHRC="${OLD_ZSHRC}-$(date +%Y-%m-%d_%H-%M-%S)"
  90. if [ -e "$OLD_OLD_ZSHRC" ]; then
  91. error "$OLD_OLD_ZSHRC exists. Can't back up ${OLD_ZSHRC}"
  92. error "re-run the installer again in a couple of seconds"
  93. exit 1
  94. fi
  95. mv "$OLD_ZSHRC" "${OLD_OLD_ZSHRC}"
  96. echo "${YELLOW}Found old ~/.zshrc.pre-oh-my-zsh." \
  97. "${GREEN}Backing up to ${OLD_OLD_ZSHRC}${RESET}"
  98. fi
  99. echo "${YELLOW}Found ~/.zshrc.${RESET} ${GREEN}Backing up to ${OLD_ZSHRC}${RESET}"
  100. mv ~/.zshrc "$OLD_ZSHRC"
  101. fi
  102. echo "${BLUE}Using the Oh My Zsh template file and adding it to ~/.zshrc.${RESET}"
  103. cp "$ZSH/templates/zshrc.zsh-template" ~/.zshrc
  104. sed "/^export ZSH=/ c\\
  105. export ZSH=\"$ZSH\"
  106. " ~/.zshrc > ~/.zshrc-omztemp
  107. mv -f ~/.zshrc-omztemp ~/.zshrc
  108. }
  109. setup_shell() {
  110. # Skip setup if the user wants or stdin is closed (not running interactively).
  111. if [ $CHSH = no ] || ! [ -t 0 ]; then
  112. return
  113. fi
  114. # If this user's login shell is already "zsh", do not attempt to switch.
  115. if [ "$(basename "$SHELL")" = "zsh" ]; then
  116. return
  117. fi
  118. # If this platform doesn't provide a "chsh" command, bail out.
  119. if ! command_exists chsh; then
  120. cat <<-EOF
  121. I can't change your shell automatically because this system does not have chsh.
  122. ${BLUE}Please manually change your default shell to zsh${RESET}
  123. EOF
  124. return
  125. fi
  126. echo "${BLUE}Time to change your default shell to zsh!${RESET}"
  127. # Test for the right location of the "shells" file
  128. if [ -f /etc/shells ]; then
  129. shells_file=/etc/shells
  130. elif [ -f /usr/share/defaults/etc/shells ]; then # Solus OS
  131. shells_file=/usr/share/defaults/etc/shells
  132. else
  133. error "could not find /etc/shells file. Change your default shell manually."
  134. return
  135. fi
  136. # Get the path to the right zsh binary
  137. # 1. Use the most preceding one based on $PATH, then check that it's in the shells file
  138. # 2. If that fails, get a zsh path from the shells file, then check it actually exists
  139. if ! zsh=$(which zsh) || ! grep -qx "$zsh" "$shells_file"; then
  140. if ! zsh=$(grep '^/.*/zsh$' "$shells_file" | tail -1) || [ ! -f "$zsh" ]; then
  141. error "no available zsh binary found. Change your default shell manually."
  142. return
  143. fi
  144. fi
  145. # Actually change the default shell to zsh
  146. if ! chsh -s "$zsh"; then
  147. error "chsh command unsuccessful. Change your default shell manually."
  148. fi
  149. }
  150. main() {
  151. # Parse arguments
  152. while [ $# -gt 0 ]; do
  153. case $1 in
  154. --skip-chsh) CHSH=no ;;
  155. esac
  156. shift
  157. done
  158. setup_color
  159. if ! command_exists zsh; then
  160. echo "${YELLOW}Zsh is not installed.${RESET} Please install zsh first."
  161. exit 1
  162. fi
  163. if [ -d "$ZSH" ]; then
  164. cat <<-EOF
  165. ${YELLOW}You already have Oh My Zsh installed.${RESET}
  166. You'll need to remove '$ZSH' if you want to reinstall.
  167. EOF
  168. exit 1
  169. fi
  170. setup_ohmyzsh
  171. setup_zshrc
  172. setup_shell
  173. printf "$GREEN"
  174. cat <<-'EOF'
  175. __ __
  176. ____ / /_ ____ ___ __ __ ____ _____/ /_
  177. / __ \/ __ \ / __ `__ \/ / / / /_ / / ___/ __ \
  178. / /_/ / / / / / / / / / / /_/ / / /_(__ ) / / /
  179. \____/_/ /_/ /_/ /_/ /_/\__, / /___/____/_/ /_/
  180. /____/ ....is now installed!
  181. Please look over the ~/.zshrc file to select plugins, themes, and options.
  182. p.s. Follow us on https://twitter.com/ohmyzsh
  183. p.p.s. Get stickers, shirts, and coffee mugs at https://shop.planetargon.com/collections/oh-my-zsh
  184. EOF
  185. printf "$RESET"
  186. exec zsh -l
  187. }
  188. main "$@"