install.sh 7.1 KB

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