install.sh 5.1 KB

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