install.sh 8.2 KB

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