install.sh 6.7 KB

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