Thetechrobo

OhMyZsh install.sh

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