install.sh 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. setup_color() {
  48. # Only use colors if connected to a terminal
  49. if [ -t 1 ]; then
  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. else
  57. RED=""
  58. GREEN=""
  59. YELLOW=""
  60. BLUE=""
  61. BOLD=""
  62. RESET=""
  63. fi
  64. }
  65. setup_ohmyzsh() {
  66. # Prevent the cloned repository from having insecure permissions. Failing to do
  67. # so causes compinit() calls to fail with "command not found: compdef" errors
  68. # for users with insecure umasks (e.g., "002", allowing group writability). Note
  69. # that this will be ignored under Cygwin by default, as Windows ACLs take
  70. # precedence over umasks except for filesystems mounted with option "noacl".
  71. umask g-w,o-w
  72. echo "${BLUE}Cloning Oh My Zsh...${RESET}"
  73. command_exists git || {
  74. error "git is not installed"
  75. exit 1
  76. }
  77. if [ "$OSTYPE" = cygwin ] && git --version | grep -q msysgit; then
  78. error "Windows/MSYS Git is not supported on Cygwin"
  79. error "Make sure the Cygwin git package is installed and is first on the \$PATH"
  80. exit 1
  81. fi
  82. git clone -c core.eol=lf -c core.autocrlf=false \
  83. -c fsck.zeroPaddedFilemode=ignore \
  84. -c fetch.fsck.zeroPaddedFilemode=ignore \
  85. -c receive.fsck.zeroPaddedFilemode=ignore \
  86. --depth=1 --branch "$BRANCH" "$REMOTE" "$ZSH" || {
  87. error "git clone of oh-my-zsh repo failed"
  88. exit 1
  89. }
  90. echo
  91. }
  92. setup_zshrc() {
  93. # Keep most recent old .zshrc at .zshrc.pre-oh-my-zsh, and older ones
  94. # with datestamp of installation that moved them aside, so we never actually
  95. # destroy a user's original zshrc
  96. echo "${BLUE}Looking for an existing zsh config...${RESET}"
  97. # Must use this exact name so uninstall.sh can find it
  98. OLD_ZSHRC=~/.zshrc.pre-oh-my-zsh
  99. if [ -f ~/.zshrc ] || [ -h ~/.zshrc ]; then
  100. if [ -e "$OLD_ZSHRC" ]; then
  101. OLD_OLD_ZSHRC="${OLD_ZSHRC}-$(date +%Y-%m-%d_%H-%M-%S)"
  102. if [ -e "$OLD_OLD_ZSHRC" ]; then
  103. error "$OLD_OLD_ZSHRC exists. Can't back up ${OLD_ZSHRC}"
  104. error "re-run the installer again in a couple of seconds"
  105. exit 1
  106. fi
  107. mv "$OLD_ZSHRC" "${OLD_OLD_ZSHRC}"
  108. echo "${YELLOW}Found old ~/.zshrc.pre-oh-my-zsh." \
  109. "${GREEN}Backing up to ${OLD_OLD_ZSHRC}${RESET}"
  110. fi
  111. echo "${YELLOW}Found ~/.zshrc.${RESET} ${GREEN}Backing up to ${OLD_ZSHRC}${RESET}"
  112. mv ~/.zshrc "$OLD_ZSHRC"
  113. fi
  114. echo "${GREEN}Using the Oh My Zsh template file and adding it to ~/.zshrc.${RESET}"
  115. cp "$ZSH/templates/zshrc.zsh-template" ~/.zshrc
  116. sed "/^export ZSH=/ c\\
  117. export ZSH=\"$ZSH\"
  118. " ~/.zshrc > ~/.zshrc-omztemp
  119. mv -f ~/.zshrc-omztemp ~/.zshrc
  120. echo
  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. # Prompt for user choice on changing the default login shell
  141. printf "${YELLOW}Do you want to change your default shell to zsh? [Y/n]${RESET} "
  142. read opt
  143. case $opt in
  144. y*|Y*|"") echo "Changing the shell..." ;;
  145. n*|N*) echo "Shell change skipped."; return ;;
  146. *) echo "Invalid choice. Shell change skipped."; return ;;
  147. esac
  148. # Check if we're running on Termux
  149. case "$PREFIX" in
  150. *com.termux*) termux=true; zsh=zsh ;;
  151. *) termux=false ;;
  152. esac
  153. if [ "$termux" != true ]; then
  154. # Test for the right location of the "shells" file
  155. if [ -f /etc/shells ]; then
  156. shells_file=/etc/shells
  157. elif [ -f /usr/share/defaults/etc/shells ]; then # Solus OS
  158. shells_file=/usr/share/defaults/etc/shells
  159. else
  160. error "could not find /etc/shells file. Change your default shell manually."
  161. return
  162. fi
  163. # Get the path to the right zsh binary
  164. # 1. Use the most preceding one based on $PATH, then check that it's in the shells file
  165. # 2. If that fails, get a zsh path from the shells file, then check it actually exists
  166. if ! zsh=$(which zsh) || ! grep -qx "$zsh" "$shells_file"; then
  167. if ! zsh=$(grep '^/.*/zsh$' "$shells_file" | tail -1) || [ ! -f "$zsh" ]; then
  168. error "no zsh binary found or not present in '$shells_file'"
  169. error "change your default shell manually."
  170. return
  171. fi
  172. fi
  173. fi
  174. # We're going to change the default shell, so back up the current one
  175. if [ -n "$SHELL" ]; then
  176. echo $SHELL > ~/.shell.pre-oh-my-zsh
  177. else
  178. grep "^$USER:" /etc/passwd | awk -F: '{print $7}' > ~/.shell.pre-oh-my-zsh
  179. fi
  180. # Actually change the default shell to zsh
  181. if ! chsh -s "$zsh"; then
  182. error "chsh command unsuccessful. Change your default shell manually."
  183. else
  184. export SHELL="$zsh"
  185. echo "${GREEN}Shell successfully changed to '$zsh'.${RESET}"
  186. fi
  187. echo
  188. }
  189. main() {
  190. # Run as unattended if stdin is closed
  191. if [ ! -t 0 ]; then
  192. RUNZSH=no
  193. CHSH=no
  194. fi
  195. # Parse arguments
  196. while [ $# -gt 0 ]; do
  197. case $1 in
  198. --unattended) RUNZSH=no; CHSH=no ;;
  199. --skip-chsh) CHSH=no ;;
  200. esac
  201. shift
  202. done
  203. setup_color
  204. if ! command_exists zsh; then
  205. echo "${YELLOW}Zsh is not installed.${RESET} Please install zsh first."
  206. exit 1
  207. fi
  208. if [ -d "$ZSH" ]; then
  209. cat <<-EOF
  210. ${YELLOW}You already have Oh My Zsh installed.${RESET}
  211. You'll need to remove '$ZSH' if you want to reinstall.
  212. EOF
  213. exit 1
  214. fi
  215. setup_ohmyzsh
  216. setup_zshrc
  217. setup_shell
  218. printf "$GREEN"
  219. cat <<-'EOF'
  220. __ __
  221. ____ / /_ ____ ___ __ __ ____ _____/ /_
  222. / __ \/ __ \ / __ `__ \/ / / / /_ / / ___/ __ \
  223. / /_/ / / / / / / / / / / /_/ / / /_(__ ) / / /
  224. \____/_/ /_/ /_/ /_/ /_/\__, / /___/____/_/ /_/
  225. /____/ ....is now installed!
  226. Please look over the ~/.zshrc file to select plugins, themes, and options.
  227. p.s. Follow us on https://twitter.com/ohmyzsh
  228. p.p.s. Get stickers, shirts, and coffee mugs at https://shop.planetargon.com/collections/oh-my-zsh
  229. EOF
  230. printf "$RESET"
  231. if [ $RUNZSH = no ]; then
  232. echo "${YELLOW}Run zsh to try it out.${RESET}"
  233. exit
  234. fi
  235. exec zsh -l
  236. }
  237. main "$@"