install.sh 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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 --depth=1 --branch "$BRANCH" "$REMOTE" "$ZSH" || {
  83. error "git clone of oh-my-zsh repo failed"
  84. exit 1
  85. }
  86. echo
  87. }
  88. setup_zshrc() {
  89. # Keep most recent old .zshrc at .zshrc.pre-oh-my-zsh, and older ones
  90. # with datestamp of installation that moved them aside, so we never actually
  91. # destroy a user's original zshrc
  92. echo "${BLUE}Looking for an existing zsh config...${RESET}"
  93. # Must use this exact name so uninstall.sh can find it
  94. OLD_ZSHRC=~/.zshrc.pre-oh-my-zsh
  95. if [ -f ~/.zshrc ] || [ -h ~/.zshrc ]; then
  96. if [ -e "$OLD_ZSHRC" ]; then
  97. OLD_OLD_ZSHRC="${OLD_ZSHRC}-$(date +%Y-%m-%d_%H-%M-%S)"
  98. if [ -e "$OLD_OLD_ZSHRC" ]; then
  99. error "$OLD_OLD_ZSHRC exists. Can't back up ${OLD_ZSHRC}"
  100. error "re-run the installer again in a couple of seconds"
  101. exit 1
  102. fi
  103. mv "$OLD_ZSHRC" "${OLD_OLD_ZSHRC}"
  104. echo "${YELLOW}Found old ~/.zshrc.pre-oh-my-zsh." \
  105. "${GREEN}Backing up to ${OLD_OLD_ZSHRC}${RESET}"
  106. fi
  107. echo "${YELLOW}Found ~/.zshrc.${RESET} ${GREEN}Backing up to ${OLD_ZSHRC}${RESET}"
  108. mv ~/.zshrc "$OLD_ZSHRC"
  109. fi
  110. echo "${GREEN}Using the Oh My Zsh template file and adding it to ~/.zshrc.${RESET}"
  111. cp "$ZSH/templates/zshrc.zsh-template" ~/.zshrc
  112. sed "/^export ZSH=/ c\\
  113. export ZSH=\"$ZSH\"
  114. " ~/.zshrc > ~/.zshrc-omztemp
  115. mv -f ~/.zshrc-omztemp ~/.zshrc
  116. echo
  117. }
  118. setup_shell() {
  119. # Skip setup if the user wants or stdin is closed (not running interactively).
  120. if [ $CHSH = no ]; then
  121. return
  122. fi
  123. # If this user's login shell is already "zsh", do not attempt to switch.
  124. if [ "$(basename "$SHELL")" = "zsh" ]; then
  125. return
  126. fi
  127. # If this platform doesn't provide a "chsh" command, bail out.
  128. if ! command_exists chsh; then
  129. cat <<-EOF
  130. I can't change your shell automatically because this system does not have chsh.
  131. ${BLUE}Please manually change your default shell to zsh${RESET}
  132. EOF
  133. return
  134. fi
  135. echo "${BLUE}Time to change your default shell to zsh:${RESET}"
  136. # Prompt for user choice on changing the default login shell
  137. printf "${YELLOW}Do you want to change your default shell to zsh? [Y/n]${RESET} "
  138. read opt
  139. case $opt in
  140. y*|Y*|"") echo "Changing the shell..." ;;
  141. n*|N*) echo "Shell change skipped."; return ;;
  142. *) echo "Invalid choice. Shell change skipped."; return ;;
  143. esac
  144. # Check if we're running on Termux
  145. case "$PREFIX" in
  146. *com.termux*) termux=true; zsh=zsh ;;
  147. *) termux=false ;;
  148. esac
  149. if [ "$termux" != true ]; then
  150. # Test for the right location of the "shells" file
  151. if [ -f /etc/shells ]; then
  152. shells_file=/etc/shells
  153. elif [ -f /usr/share/defaults/etc/shells ]; then # Solus OS
  154. shells_file=/usr/share/defaults/etc/shells
  155. else
  156. error "could not find /etc/shells file. Change your default shell manually."
  157. return
  158. fi
  159. # Get the path to the right zsh binary
  160. # 1. Use the most preceding one based on $PATH, then check that it's in the shells file
  161. # 2. If that fails, get a zsh path from the shells file, then check it actually exists
  162. if ! zsh=$(which zsh) || ! grep -qx "$zsh" "$shells_file"; then
  163. if ! zsh=$(grep '^/.*/zsh$' "$shells_file" | tail -1) || [ ! -f "$zsh" ]; then
  164. error "no zsh binary found or not present in '$shells_file'"
  165. error "change your default shell manually."
  166. return
  167. fi
  168. fi
  169. fi
  170. # We're going to change the default shell, so back up the current one
  171. if [ -n "$SHELL" ]; then
  172. echo $SHELL > ~/.shell.pre-oh-my-zsh
  173. else
  174. grep "^$USER:" /etc/passwd | awk -F: '{print $7}' > ~/.shell.pre-oh-my-zsh
  175. fi
  176. # Actually change the default shell to zsh
  177. if ! chsh -s "$zsh"; then
  178. error "chsh command unsuccessful. Change your default shell manually."
  179. else
  180. export SHELL="$zsh"
  181. echo "${GREEN}Shell successfully changed to '$zsh'.${RESET}"
  182. fi
  183. echo
  184. }
  185. main() {
  186. # Run as unattended if stdin is closed
  187. if [ ! -t 0 ]; then
  188. RUNZSH=no
  189. CHSH=no
  190. fi
  191. # Parse arguments
  192. while [ $# -gt 0 ]; do
  193. case $1 in
  194. --unattended) RUNZSH=no; CHSH=no ;;
  195. --skip-chsh) CHSH=no ;;
  196. esac
  197. shift
  198. done
  199. setup_color
  200. if ! command_exists zsh; then
  201. echo "${YELLOW}Zsh is not installed.${RESET} Please install zsh first."
  202. exit 1
  203. fi
  204. if [ -d "$ZSH" ]; then
  205. cat <<-EOF
  206. ${YELLOW}You already have Oh My Zsh installed.${RESET}
  207. You'll need to remove '$ZSH' if you want to reinstall.
  208. EOF
  209. exit 1
  210. fi
  211. setup_ohmyzsh
  212. setup_zshrc
  213. setup_shell
  214. printf "$GREEN"
  215. cat <<-'EOF'
  216. __ __
  217. ____ / /_ ____ ___ __ __ ____ _____/ /_
  218. / __ \/ __ \ / __ `__ \/ / / / /_ / / ___/ __ \
  219. / /_/ / / / / / / / / / / /_/ / / /_(__ ) / / /
  220. \____/_/ /_/ /_/ /_/ /_/\__, / /___/____/_/ /_/
  221. /____/ ....is now installed!
  222. Please look over the ~/.zshrc file to select plugins, themes, and options.
  223. p.s. Follow us on https://twitter.com/ohmyzsh
  224. p.p.s. Get stickers, shirts, and coffee mugs at https://shop.planetargon.com/collections/oh-my-zsh
  225. EOF
  226. printf "$RESET"
  227. if [ $RUNZSH = no ]; then
  228. echo "${YELLOW}Run zsh to try it out.${RESET}"
  229. exit
  230. fi
  231. exec zsh -l
  232. }
  233. main "$@"