From 9a832cccef79084e02028ca2fa8b021817256ad4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Cornell=C3=A0?= Date: Sun, 29 May 2016 10:35:55 +0200 Subject: [PATCH 01/25] installer: use exit-on-error from the beginning --- tools/install.sh | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/tools/install.sh b/tools/install.sh index 2e5e8fd43..5c5419b17 100755 --- a/tools/install.sh +++ b/tools/install.sh @@ -1,8 +1,16 @@ +#!/bin/sh +set -e + +# Test command existence (POSIX compatible) +command_exists() { + command -v "$@" >/dev/null 2>&1 +} + main() { # Use colors, but only if connected to a terminal, and that terminal # supports them. - if which tput >/dev/null 2>&1; then - ncolors=$(tput colors) + if command_exists tput; then + ncolors=$(tput colors) fi if [ -t 1 ] && [ -n "$ncolors" ] && [ "$ncolors" -ge 8 ]; then RED="$(tput setaf 1)" @@ -20,13 +28,9 @@ main() { NORMAL="" fi - # Only enable exit-on-error after the non-critical colorization stuff, - # which may fail on systems lacking tput or terminfo - set -e - - if ! command -v zsh >/dev/null 2>&1; then + if ! command_exists zsh; then printf "${YELLOW}Zsh is not installed!${NORMAL} Please install zsh first!\n" - exit + exit 1 fi if [ ! -n "$ZSH" ]; then @@ -36,7 +40,7 @@ main() { if [ -d "$ZSH" ]; then printf "${YELLOW}You already have Oh My Zsh installed.${NORMAL}\n" printf "You'll need to remove $ZSH if you want to re-install.\n" - exit + exit 1 fi # Prevent the cloned repository from having insecure permissions. Failing to do @@ -47,7 +51,7 @@ main() { umask g-w,o-w printf "${BLUE}Cloning Oh My Zsh...${NORMAL}\n" - command -v git >/dev/null 2>&1 || { + command_exists git || { echo "Error: git is not installed" exit 1 } @@ -82,7 +86,7 @@ main() { TEST_CURRENT_SHELL=$(basename "$SHELL") if [ "$TEST_CURRENT_SHELL" != "zsh" ]; then # If this platform provides a "chsh" command (not Cygwin), do it, man! - if hash chsh >/dev/null 2>&1; then + if command_exists chsh; then printf "${BLUE}Time to change your default shell to zsh!${NORMAL}\n" chsh -s $(grep /zsh$ /etc/shells | tail -1) # Else, suggest the user do so manually. From 73ef051aaecd5a63b84585bf67e263642660abb4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Cornell=C3=A0?= Date: Tue, 9 Aug 2016 02:28:47 +0200 Subject: [PATCH 02/25] installer: use tabs to allow future heredocs This will allow us to use tab stripping heredocs with `<<-'. See http://www.tldp.org/LDP/abs/html/here-docs.html#EX71A Add editorconfig file to enforce this style. See http://editorconfig.org for more information. --- .editorconfig | 10 +++ tools/install.sh | 194 +++++++++++++++++++++++------------------------ 2 files changed, 107 insertions(+), 97 deletions(-) create mode 100644 .editorconfig diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 000000000..aa18e0e5c --- /dev/null +++ b/.editorconfig @@ -0,0 +1,10 @@ +root = true + +[*] +end_of_line = lf +insert_final_newline = true +charset = utf-8 + +[*.sh] +indent_size = 4 +indent_style = tab diff --git a/tools/install.sh b/tools/install.sh index 5c5419b17..b1327af41 100755 --- a/tools/install.sh +++ b/tools/install.sh @@ -3,116 +3,116 @@ set -e # Test command existence (POSIX compatible) command_exists() { - command -v "$@" >/dev/null 2>&1 + command -v "$@" >/dev/null 2>&1 } main() { - # Use colors, but only if connected to a terminal, and that terminal - # supports them. - if command_exists tput; then - ncolors=$(tput colors) - fi - if [ -t 1 ] && [ -n "$ncolors" ] && [ "$ncolors" -ge 8 ]; then - RED="$(tput setaf 1)" - GREEN="$(tput setaf 2)" - YELLOW="$(tput setaf 3)" - BLUE="$(tput setaf 4)" - BOLD="$(tput bold)" - NORMAL="$(tput sgr0)" - else - RED="" - GREEN="" - YELLOW="" - BLUE="" - BOLD="" - NORMAL="" - fi + # Use colors, but only if connected to a terminal, and that terminal + # supports them. + if command_exists tput; then + ncolors=$(tput colors) + fi - if ! command_exists zsh; then - printf "${YELLOW}Zsh is not installed!${NORMAL} Please install zsh first!\n" - exit 1 - fi + if [ -t 1 ] && [ -n "$ncolors" ] && [ "$ncolors" -ge 8 ]; then + RED="$(tput setaf 1)" + GREEN="$(tput setaf 2)" + YELLOW="$(tput setaf 3)" + BLUE="$(tput setaf 4)" + BOLD="$(tput bold)" + NORMAL="$(tput sgr0)" + else + RED="" + GREEN="" + YELLOW="" + BLUE="" + BOLD="" + NORMAL="" + fi - if [ ! -n "$ZSH" ]; then - ZSH=~/.oh-my-zsh - fi + if ! command_exists zsh; then + printf "${YELLOW}Zsh is not installed!${NORMAL} Please install zsh first!\n" + exit 1 + fi - if [ -d "$ZSH" ]; then - printf "${YELLOW}You already have Oh My Zsh installed.${NORMAL}\n" - printf "You'll need to remove $ZSH if you want to re-install.\n" - exit 1 - fi + if [ ! -n "$ZSH" ]; then + ZSH=~/.oh-my-zsh + fi - # Prevent the cloned repository from having insecure permissions. Failing to do - # so causes compinit() calls to fail with "command not found: compdef" errors - # for users with insecure umasks (e.g., "002", allowing group writability). Note - # that this will be ignored under Cygwin by default, as Windows ACLs take - # precedence over umasks except for filesystems mounted with option "noacl". - umask g-w,o-w + if [ -d "$ZSH" ]; then + printf "${YELLOW}You already have Oh My Zsh installed.${NORMAL}\n" + printf "You'll need to remove $ZSH if you want to re-install.\n" + exit 1 + fi - printf "${BLUE}Cloning Oh My Zsh...${NORMAL}\n" - command_exists git || { - echo "Error: git is not installed" - exit 1 - } - # The Windows (MSYS) Git is not compatible with normal use on cygwin - if [ "$OSTYPE" = cygwin ]; then - if git --version | grep msysgit > /dev/null; then - echo "Error: Windows/MSYS Git is not supported on Cygwin" - echo "Error: Make sure the Cygwin git package is installed and is first on the path" - exit 1 - fi - fi - env git clone --depth=1 https://github.com/robbyrussell/oh-my-zsh.git "$ZSH" || { - printf "Error: git clone of oh-my-zsh repo failed\n" - exit 1 - } + # Prevent the cloned repository from having insecure permissions. Failing to do + # so causes compinit() calls to fail with "command not found: compdef" errors + # for users with insecure umasks (e.g., "002", allowing group writability). Note + # that this will be ignored under Cygwin by default, as Windows ACLs take + # precedence over umasks except for filesystems mounted with option "noacl". + umask g-w,o-w + printf "${BLUE}Cloning Oh My Zsh...${NORMAL}\n" + command_exists git || { + echo "Error: git is not installed" + exit 1 + } + # The Windows (MSYS) Git is not compatible with normal use on cygwin + if [ "$OSTYPE" = cygwin ]; then + if git --version | grep msysgit > /dev/null; then + echo "Error: Windows/MSYS Git is not supported on Cygwin" + echo "Error: Make sure the Cygwin git package is installed and is first on the path" + exit 1 + fi + fi + env git clone --depth=1 https://github.com/robbyrussell/oh-my-zsh.git "$ZSH" || { + printf "Error: git clone of oh-my-zsh repo failed\n" + exit 1 + } - printf "${BLUE}Looking for an existing zsh config...${NORMAL}\n" - if [ -f ~/.zshrc ] || [ -h ~/.zshrc ]; then - printf "${YELLOW}Found ~/.zshrc.${NORMAL} ${GREEN}Backing up to ~/.zshrc.pre-oh-my-zsh${NORMAL}\n"; - mv ~/.zshrc ~/.zshrc.pre-oh-my-zsh; - fi + printf "${BLUE}Looking for an existing zsh config...${NORMAL}\n" + if [ -f ~/.zshrc ] || [ -h ~/.zshrc ]; then + printf "${YELLOW}Found ~/.zshrc.${NORMAL} ${GREEN}Backing up to ~/.zshrc.pre-oh-my-zsh${NORMAL}\n"; + mv ~/.zshrc ~/.zshrc.pre-oh-my-zsh; + fi - printf "${BLUE}Using the Oh My Zsh template file and adding it to ~/.zshrc${NORMAL}\n" - cp "$ZSH"/templates/zshrc.zsh-template ~/.zshrc - sed "/^export ZSH=/ c\\ - export ZSH=\"$ZSH\" - " ~/.zshrc > ~/.zshrc-omztemp - mv -f ~/.zshrc-omztemp ~/.zshrc + printf "${BLUE}Using the Oh My Zsh template file and adding it to ~/.zshrc${NORMAL}\n" + cp "$ZSH/templates/zshrc.zsh-template" ~/.zshrc + sed "/^export ZSH=/ c\\ +export ZSH=\"$ZSH\" +" ~/.zshrc > ~/.zshrc-omztemp + mv -f ~/.zshrc-omztemp ~/.zshrc - # If this user's login shell is not already "zsh", attempt to switch. - TEST_CURRENT_SHELL=$(basename "$SHELL") - if [ "$TEST_CURRENT_SHELL" != "zsh" ]; then - # If this platform provides a "chsh" command (not Cygwin), do it, man! - if command_exists chsh; then - printf "${BLUE}Time to change your default shell to zsh!${NORMAL}\n" - chsh -s $(grep /zsh$ /etc/shells | tail -1) - # Else, suggest the user do so manually. - else - printf "I can't change your shell automatically because this system does not have chsh.\n" - printf "${BLUE}Please manually change your default shell to zsh!${NORMAL}\n" - fi - fi + # If this user's login shell is not already "zsh", attempt to switch. + TEST_CURRENT_SHELL=$(basename "$SHELL") + if [ "$TEST_CURRENT_SHELL" != "zsh" ]; then + # If this platform provides a "chsh" command (not Cygwin), do it, man! + if command_exists chsh; then + printf "${BLUE}Time to change your default shell to zsh!${NORMAL}\n" + chsh -s $(grep /zsh$ /etc/shells | tail -1) + # Else, suggest the user do so manually. + else + printf "I can't change your shell automatically because this system does not have chsh.\n" + printf "${BLUE}Please manually change your default shell to zsh!${NORMAL}\n" + fi + fi - printf "${GREEN}" - echo ' __ __ ' - echo ' ____ / /_ ____ ___ __ __ ____ _____/ /_ ' - echo ' / __ \/ __ \ / __ `__ \/ / / / /_ / / ___/ __ \ ' - echo '/ /_/ / / / / / / / / / / /_/ / / /_(__ ) / / / ' - echo '\____/_/ /_/ /_/ /_/ /_/\__, / /___/____/_/ /_/ ' - echo ' /____/ ....is now installed!' - echo '' - echo '' - echo 'Please look over the ~/.zshrc file to select plugins, themes, and options.' - echo '' - echo 'p.s. Follow us at https://twitter.com/ohmyzsh' - echo '' - echo 'p.p.s. Get stickers, shirts, and coffee mugs at https://shop.planetargon.com/collections/oh-my-zsh' - echo '' - printf "${NORMAL}" - env zsh -l + printf "${GREEN}" + echo ' __ __ ' + echo ' ____ / /_ ____ ___ __ __ ____ _____/ /_ ' + echo ' / __ \/ __ \ / __ `__ \/ / / / /_ / / ___/ __ \ ' + echo '/ /_/ / / / / / / / / / / /_/ / / /_(__ ) / / / ' + echo '\____/_/ /_/ /_/ /_/ /_/\__, / /___/____/_/ /_/ ' + echo ' /____/ ....is now installed!' + echo '' + echo '' + echo 'Please look over the ~/.zshrc file to select plugins, themes, and options.' + echo '' + echo 'p.s. Follow us at https://twitter.com/ohmyzsh' + echo '' + echo 'p.p.s. Get stickers, shirts, and coffee mugs at https://shop.planetargon.com/collections/oh-my-zsh' + echo '' + printf "${NORMAL}" + env zsh -l } main From f94443925dacf44ad24e0efef9e486afb1c9c4d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Cornell=C3=A0?= Date: Mon, 30 May 2016 18:05:29 +0200 Subject: [PATCH 03/25] installer: reorganise and add comments --- tools/install.sh | 97 ++++++++++++++++++++++++++++-------------------- 1 file changed, 57 insertions(+), 40 deletions(-) diff --git a/tools/install.sh b/tools/install.sh index b1327af41..f7eee4dab 100755 --- a/tools/install.sh +++ b/tools/install.sh @@ -1,7 +1,18 @@ #!/bin/sh +# +# This script should be run via curl: +# sh -c "$(curl -fsSL https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh)" +# or wget: +# sh -c "$(wget -qO- https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh)" +# +# As an alternative, you can download the install script separately and +# run it afterwards with `sh install.sh' +# set -e -# Test command existence (POSIX compatible) +# Default location +ZSH=${ZSH:-~/.oh-my-zsh} + command_exists() { command -v "$@" >/dev/null 2>&1 } @@ -30,17 +41,15 @@ main() { fi if ! command_exists zsh; then - printf "${YELLOW}Zsh is not installed!${NORMAL} Please install zsh first!\n" + echo "${YELLOW}Zsh is not installed!${NORMAL} Please install zsh first!" exit 1 fi - if [ ! -n "$ZSH" ]; then - ZSH=~/.oh-my-zsh - fi - if [ -d "$ZSH" ]; then - printf "${YELLOW}You already have Oh My Zsh installed.${NORMAL}\n" - printf "You'll need to remove $ZSH if you want to re-install.\n" + cat <<-EOF + ${YELLOW}You already have Oh My Zsh installed.${NORMAL} + You'll need to remove $ZSH if you want to re-install. + EOF exit 1 fi @@ -51,31 +60,34 @@ main() { # precedence over umasks except for filesystems mounted with option "noacl". umask g-w,o-w - printf "${BLUE}Cloning Oh My Zsh...${NORMAL}\n" + echo "${BLUE}Cloning Oh My Zsh...${NORMAL}" + command_exists git || { echo "Error: git is not installed" exit 1 } - # The Windows (MSYS) Git is not compatible with normal use on cygwin - if [ "$OSTYPE" = cygwin ]; then - if git --version | grep msysgit > /dev/null; then - echo "Error: Windows/MSYS Git is not supported on Cygwin" - echo "Error: Make sure the Cygwin git package is installed and is first on the path" - exit 1 - fi + + if [ "$OSTYPE" = cygwin ] && git --version | grep -q msysgit; then + cat <<-EOF + Error: Windows/MSYS Git is not supported on Cygwin + Error: Make sure the Cygwin git package is installed and is first on the $PATH + EOF + exit 1 fi - env git clone --depth=1 https://github.com/robbyrussell/oh-my-zsh.git "$ZSH" || { - printf "Error: git clone of oh-my-zsh repo failed\n" + + git clone --depth=1 https://github.com/robbyrussell/oh-my-zsh.git "$ZSH" || { + echo "Error: git clone of oh-my-zsh repo failed" exit 1 } - printf "${BLUE}Looking for an existing zsh config...${NORMAL}\n" + echo "${BLUE}Looking for an existing zsh config...${NORMAL}" if [ -f ~/.zshrc ] || [ -h ~/.zshrc ]; then - printf "${YELLOW}Found ~/.zshrc.${NORMAL} ${GREEN}Backing up to ~/.zshrc.pre-oh-my-zsh${NORMAL}\n"; + echo "${YELLOW}Found ~/.zshrc.${GREEN} Backing up to ~/.zshrc.pre-oh-my-zsh.${NORMAL}" mv ~/.zshrc ~/.zshrc.pre-oh-my-zsh; fi - printf "${BLUE}Using the Oh My Zsh template file and adding it to ~/.zshrc${NORMAL}\n" + echo "${BLUE}Using the Oh My Zsh template file and adding it to ~/.zshrc.${NORMAL}" + cp "$ZSH/templates/zshrc.zsh-template" ~/.zshrc sed "/^export ZSH=/ c\\ export ZSH=\"$ZSH\" @@ -87,31 +99,36 @@ export ZSH=\"$ZSH\" if [ "$TEST_CURRENT_SHELL" != "zsh" ]; then # If this platform provides a "chsh" command (not Cygwin), do it, man! if command_exists chsh; then - printf "${BLUE}Time to change your default shell to zsh!${NORMAL}\n" + echo "${BLUE}Time to change your default shell to zsh!${NORMAL}" chsh -s $(grep /zsh$ /etc/shells | tail -1) # Else, suggest the user do so manually. else - printf "I can't change your shell automatically because this system does not have chsh.\n" - printf "${BLUE}Please manually change your default shell to zsh!${NORMAL}\n" + cat <<-EOF + I can't change your shell automatically because this system does not have chsh. + ${BLUE}Please manually change your default shell to zsh${NORMAL} + EOF fi fi - printf "${GREEN}" - echo ' __ __ ' - echo ' ____ / /_ ____ ___ __ __ ____ _____/ /_ ' - echo ' / __ \/ __ \ / __ `__ \/ / / / /_ / / ___/ __ \ ' - echo '/ /_/ / / / / / / / / / / /_/ / / /_(__ ) / / / ' - echo '\____/_/ /_/ /_/ /_/ /_/\__, / /___/____/_/ /_/ ' - echo ' /____/ ....is now installed!' - echo '' - echo '' - echo 'Please look over the ~/.zshrc file to select plugins, themes, and options.' - echo '' - echo 'p.s. Follow us at https://twitter.com/ohmyzsh' - echo '' - echo 'p.p.s. Get stickers, shirts, and coffee mugs at https://shop.planetargon.com/collections/oh-my-zsh' - echo '' - printf "${NORMAL}" + printf "$GREEN" + cat <<-'EOF' + __ __ + ____ / /_ ____ ___ __ __ ____ _____/ /_ + / __ \/ __ \ / __ `__ \/ / / / /_ / / ___/ __ \ + / /_/ / / / / / / / / / / /_/ / / /_(__ ) / / / + \____/_/ /_/ /_/ /_/ /_/\__, / /___/____/_/ /_/ + /____/ ....is now installed! + + + Please look over the ~/.zshrc file to select plugins, themes, and options. + + p.s. Follow us on https://twitter.com/ohmyzsh + + p.p.s. Get stickers, shirts, and coffee mugs at https://shop.planetargon.com/collections/oh-my-zsh + + EOF + printf "$NORMAL" + env zsh -l } From a7bd1f99ae54ff2e2081c455f1b34900059ccc71 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Cornell=C3=A0?= Date: Sun, 29 May 2016 08:15:40 +0200 Subject: [PATCH 04/25] installer: extract most code into functions --- tools/install.sh | 52 +++++++++++++++++++++++++++++++----------------- 1 file changed, 34 insertions(+), 18 deletions(-) diff --git a/tools/install.sh b/tools/install.sh index f7eee4dab..3ed8584aa 100755 --- a/tools/install.sh +++ b/tools/install.sh @@ -17,14 +17,16 @@ command_exists() { command -v "$@" >/dev/null 2>&1 } -main() { - # Use colors, but only if connected to a terminal, and that terminal - # supports them. +# Set up color sequences +setup_color() { if command_exists tput; then ncolors=$(tput colors) + else + ncolors=0 fi - if [ -t 1 ] && [ -n "$ncolors" ] && [ "$ncolors" -ge 8 ]; then + # Only use colors if connected to a terminal that supports them + if [ -t 1 ] && [ $ncolors -ge 8 ]; then RED="$(tput setaf 1)" GREEN="$(tput setaf 2)" YELLOW="$(tput setaf 3)" @@ -39,20 +41,9 @@ main() { BOLD="" NORMAL="" fi +} - if ! command_exists zsh; then - echo "${YELLOW}Zsh is not installed!${NORMAL} Please install zsh first!" - exit 1 - fi - - if [ -d "$ZSH" ]; then - cat <<-EOF - ${YELLOW}You already have Oh My Zsh installed.${NORMAL} - You'll need to remove $ZSH if you want to re-install. - EOF - exit 1 - fi - +setup_ohmyzsh() { # Prevent the cloned repository from having insecure permissions. Failing to do # so causes compinit() calls to fail with "command not found: compdef" errors # for users with insecure umasks (e.g., "002", allowing group writability). Note @@ -79,11 +70,13 @@ main() { echo "Error: git clone of oh-my-zsh repo failed" exit 1 } +} +setup_zshrc() { echo "${BLUE}Looking for an existing zsh config...${NORMAL}" if [ -f ~/.zshrc ] || [ -h ~/.zshrc ]; then echo "${YELLOW}Found ~/.zshrc.${GREEN} Backing up to ~/.zshrc.pre-oh-my-zsh.${NORMAL}" - mv ~/.zshrc ~/.zshrc.pre-oh-my-zsh; + mv ~/.zshrc ~/.zshrc.pre-oh-my-zsh fi echo "${BLUE}Using the Oh My Zsh template file and adding it to ~/.zshrc.${NORMAL}" @@ -93,7 +86,9 @@ main() { export ZSH=\"$ZSH\" " ~/.zshrc > ~/.zshrc-omztemp mv -f ~/.zshrc-omztemp ~/.zshrc +} +setup_shell() { # If this user's login shell is not already "zsh", attempt to switch. TEST_CURRENT_SHELL=$(basename "$SHELL") if [ "$TEST_CURRENT_SHELL" != "zsh" ]; then @@ -109,6 +104,27 @@ export ZSH=\"$ZSH\" EOF fi fi +} + +main() { + setup_color + + if ! command_exists zsh; then + echo "${YELLOW}Zsh is not installed.${NORMAL} Please install zsh first." + exit 1 + fi + + if [ -d "$ZSH" ]; then + cat <<-EOF + ${YELLOW}You already have Oh My Zsh installed.${NORMAL} + You'll need to remove $ZSH if you want to reinstall. + EOF + exit 1 + fi + + setup_ohmyzsh + setup_zshrc + setup_shell printf "$GREEN" cat <<-'EOF' From 576499a5ad6bc7b4b80238010ce8f1ab6dcde264 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Cornell=C3=A0?= Date: Tue, 21 May 2019 14:15:42 +0200 Subject: [PATCH 05/25] installer: fix for failed chsh quitting the installation --- tools/install.sh | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tools/install.sh b/tools/install.sh index 3ed8584aa..446e7f416 100755 --- a/tools/install.sh +++ b/tools/install.sh @@ -95,7 +95,9 @@ setup_shell() { # If this platform provides a "chsh" command (not Cygwin), do it, man! if command_exists chsh; then echo "${BLUE}Time to change your default shell to zsh!${NORMAL}" - chsh -s $(grep /zsh$ /etc/shells | tail -1) + if ! chsh -s $(grep /zsh$ /etc/shells | tail -1); then + echo "Error: chsh command unsuccessful. Change your default shell manually." + fi # Else, suggest the user do so manually. else cat <<-EOF From 1fdd5351fbea83293c510a4457ada799ae407a8b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Cornell=C3=A0?= Date: Tue, 21 May 2019 14:47:25 +0200 Subject: [PATCH 06/25] installer: abstract error messages --- tools/install.sh | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/tools/install.sh b/tools/install.sh index 446e7f416..884080c30 100755 --- a/tools/install.sh +++ b/tools/install.sh @@ -17,6 +17,10 @@ command_exists() { command -v "$@" >/dev/null 2>&1 } +error() { + echo "Error: $@" >&2 +} + # Set up color sequences setup_color() { if command_exists tput; then @@ -54,20 +58,18 @@ setup_ohmyzsh() { echo "${BLUE}Cloning Oh My Zsh...${NORMAL}" command_exists git || { - echo "Error: git is not installed" + error "git is not installed" exit 1 } if [ "$OSTYPE" = cygwin ] && git --version | grep -q msysgit; then - cat <<-EOF - Error: Windows/MSYS Git is not supported on Cygwin - Error: Make sure the Cygwin git package is installed and is first on the $PATH - EOF + error "Windows/MSYS Git is not supported on Cygwin" + error "Make sure the Cygwin git package is installed and is first on the \$PATH" exit 1 fi git clone --depth=1 https://github.com/robbyrussell/oh-my-zsh.git "$ZSH" || { - echo "Error: git clone of oh-my-zsh repo failed" + error "git clone of oh-my-zsh repo failed" exit 1 } } @@ -96,7 +98,7 @@ setup_shell() { if command_exists chsh; then echo "${BLUE}Time to change your default shell to zsh!${NORMAL}" if ! chsh -s $(grep /zsh$ /etc/shells | tail -1); then - echo "Error: chsh command unsuccessful. Change your default shell manually." + error "chsh command unsuccessful. Change your default shell manually." fi # Else, suggest the user do so manually. else @@ -119,7 +121,7 @@ main() { if [ -d "$ZSH" ]; then cat <<-EOF ${YELLOW}You already have Oh My Zsh installed.${NORMAL} - You'll need to remove $ZSH if you want to reinstall. + You'll need to remove '$ZSH' if you want to reinstall. EOF exit 1 fi From 73f29087f99e2e6630dcc5954db1240e8c885147 Mon Sep 17 00:00:00 2001 From: Luca S Date: Thu, 19 Jul 2018 22:12:45 +0200 Subject: [PATCH 07/25] installer: replace the current shell with the new ZSH instance This replaces the currently running process with the new one using `exec` instead of creating a new process. This way, when the user `exit`s out of the new shell it will not pop them back into the shell from which ohmyzsh was installed from. --- tools/install.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/install.sh b/tools/install.sh index 884080c30..659cd9447 100755 --- a/tools/install.sh +++ b/tools/install.sh @@ -149,7 +149,7 @@ main() { EOF printf "$NORMAL" - env zsh -l + exec zsh -l } main From 8e10ac4d7346ebd4c6cf2815b72fd82c1cf980a0 Mon Sep 17 00:00:00 2001 From: Raf Czlonka Date: Mon, 8 Oct 2018 21:50:03 +0100 Subject: [PATCH 08/25] installer: make sure shell in /etc/shells is not commented Otherwise we risk a situation where a full path to `zsh` is commented, i.e.: #/usr/local/bin/zsh --- tools/install.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/install.sh b/tools/install.sh index 659cd9447..7eea2e748 100755 --- a/tools/install.sh +++ b/tools/install.sh @@ -97,7 +97,7 @@ setup_shell() { # If this platform provides a "chsh" command (not Cygwin), do it, man! if command_exists chsh; then echo "${BLUE}Time to change your default shell to zsh!${NORMAL}" - if ! chsh -s $(grep /zsh$ /etc/shells | tail -1); then + if ! chsh -s $(grep '^/.*/zsh$' /etc/shells | tail -1); then error "chsh command unsuccessful. Change your default shell manually." fi # Else, suggest the user do so manually. From 62216aaa8ff496cd1a086c70642b5fff9e7fa282 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Cornell=C3=A0?= Date: Fri, 24 May 2019 16:17:09 +0200 Subject: [PATCH 09/25] installer: use guard clauses in setup_shell for better readability Guard clauses are if constructs that return early if there is an error that prevents continuing. This way there isn't a big nesting of if expressions. --- tools/install.sh | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/tools/install.sh b/tools/install.sh index 7eea2e748..4dc25b0e0 100755 --- a/tools/install.sh +++ b/tools/install.sh @@ -91,22 +91,23 @@ export ZSH=\"$ZSH\" } setup_shell() { - # If this user's login shell is not already "zsh", attempt to switch. - TEST_CURRENT_SHELL=$(basename "$SHELL") - if [ "$TEST_CURRENT_SHELL" != "zsh" ]; then - # If this platform provides a "chsh" command (not Cygwin), do it, man! - if command_exists chsh; then - echo "${BLUE}Time to change your default shell to zsh!${NORMAL}" - if ! chsh -s $(grep '^/.*/zsh$' /etc/shells | tail -1); then - error "chsh command unsuccessful. Change your default shell manually." - fi - # Else, suggest the user do so manually. - else - cat <<-EOF - I can't change your shell automatically because this system does not have chsh. - ${BLUE}Please manually change your default shell to zsh${NORMAL} - EOF - fi + # If this user's login shell is already "zsh", do not attempt to switch. + if [ "$(basename "$SHELL")" = "zsh" ]; then + return + fi + + # If this platform doesn't provide a "chsh" command, bail out. + if ! command_exists chsh; then + cat <<-EOF + I can't change your shell automatically because this system does not have chsh. + ${BLUE}Please manually change your default shell to zsh${NORMAL} + EOF + return + fi + + echo "${BLUE}Time to change your default shell to zsh!${NORMAL}" + if ! chsh -s $(grep '^/.*/zsh$' /etc/shells | tail -1); then + error "chsh command unsuccessful. Change your default shell manually." fi } From 9d2b3ce79f9f7eaf989eb4606c36f84ee8eb1cfb Mon Sep 17 00:00:00 2001 From: Fran Date: Fri, 24 May 2019 16:42:26 +0200 Subject: [PATCH 10/25] installer: fix for Solus OS and systems with no /etc/shells Co-authored-by: Fredrik Fornwall --- tools/install.sh | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/tools/install.sh b/tools/install.sh index 4dc25b0e0..8ec973832 100755 --- a/tools/install.sh +++ b/tools/install.sh @@ -106,7 +106,18 @@ setup_shell() { fi echo "${BLUE}Time to change your default shell to zsh!${NORMAL}" - if ! chsh -s $(grep '^/.*/zsh$' /etc/shells | tail -1); then + + # Test for the right location of the "shells" file + if [ -f /etc/shells ]; then + shells_file=/etc/shells + elif [ -f /usr/share/defaults/etc/shells ]; then # Solus OS + shells_file=/usr/share/defaults/etc/shells + else + error "could not find /etc/shells file. Change your default shell manually." + return + fi + + if ! chsh -s $(grep '^/.*/zsh$' "$shells_file" | tail -1); then error "chsh command unsuccessful. Change your default shell manually." fi } From a6a093ba2aed292f0610110c0e787aca57288319 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Cornell=C3=A0?= Date: Fri, 24 May 2019 17:05:20 +0200 Subject: [PATCH 11/25] installer: improve zsh binary path search in setup_shell This changes the behavior to default to the binary found first in $PATH, then checking it's actually in the shells file (/etc/shells). If that fails go back to the previous behavior, but actually check that the path obtained exists in the filesystem. Co-authored-by: Joel Kuzmarski --- tools/install.sh | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/tools/install.sh b/tools/install.sh index 8ec973832..f91e02954 100755 --- a/tools/install.sh +++ b/tools/install.sh @@ -117,7 +117,18 @@ setup_shell() { return fi - if ! chsh -s $(grep '^/.*/zsh$' "$shells_file" | tail -1); then + # Get the path to the right zsh binary + # 1. Use the most preceding one based on $PATH, then check that it's in the shells file + # 2. If that fails, get a zsh path from the shells file, then check it actually exists + if ! zsh=$(which zsh) || ! grep -qx "$zsh" "$shells_file"; then + if ! zsh=$(grep '^/.*/zsh$' "$shells_file" | tail -1) || [ ! -f "$zsh" ]; then + error "no available zsh binary found. Change your default shell manually." + return + fi + fi + + # Actually change the default shell to zsh + if ! chsh -s "$zsh"; then error "chsh command unsuccessful. Change your default shell manually." fi } From aa27430e10b10aa1ff879722095696c66bed85ae Mon Sep 17 00:00:00 2001 From: Andrew Janke Date: Sat, 28 Feb 2015 01:46:10 -0500 Subject: [PATCH 12/25] installer: add ability to install from forked & branched repos This facilitates testing of changes to the core installation code: you'll be able to do a roundtrip test of install and uninstall using the working code on your branch. Controlled by passing $REPO and $BRANCH environment variables to install.sh. --- tools/install.sh | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/tools/install.sh b/tools/install.sh index f91e02954..0d5ad2633 100755 --- a/tools/install.sh +++ b/tools/install.sh @@ -5,13 +5,21 @@ # or wget: # sh -c "$(wget -qO- https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh)" # -# As an alternative, you can download the install script separately and -# run it afterwards with `sh install.sh' +# As an alternative, you can first download the install script and run it afterwards: +# wget https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh +# sh install.sh +# +# Respects these environment variables for tweaking the installation process: +# REPO - name of the GitHub repo to install from (default: robbyrussell/oh-my-zsh) +# BRANCH - branch to check out immediately after install # set -e -# Default location +# Default settings ZSH=${ZSH:-~/.oh-my-zsh} +REPO=${REPO:-robbyrussell/oh-my-zsh} +BRANCH=${BRANCH:-master} + command_exists() { command -v "$@" >/dev/null 2>&1 @@ -68,7 +76,7 @@ setup_ohmyzsh() { exit 1 fi - git clone --depth=1 https://github.com/robbyrussell/oh-my-zsh.git "$ZSH" || { + git clone --depth=1 --branch "$BRANCH" "https://github.com/$REPO.git" "$ZSH" || { error "git clone of oh-my-zsh repo failed" exit 1 } From 220d69b2ce18fab70b874a848ee646fe02eaa0aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Cornell=C3=A0?= Date: Sun, 26 May 2019 18:57:40 +0200 Subject: [PATCH 13/25] installer: allow configuration of remote URL to clone from --- tools/install.sh | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/tools/install.sh b/tools/install.sh index 0d5ad2633..db23d9896 100755 --- a/tools/install.sh +++ b/tools/install.sh @@ -11,16 +11,17 @@ # # Respects these environment variables for tweaking the installation process: # REPO - name of the GitHub repo to install from (default: robbyrussell/oh-my-zsh) -# BRANCH - branch to check out immediately after install +# REMOTE - full remote URL of the git repo to install (default: GitHub via HTTPS) +# BRANCH - branch to check out immediately after install (default: master) # set -e # Default settings ZSH=${ZSH:-~/.oh-my-zsh} REPO=${REPO:-robbyrussell/oh-my-zsh} +REMOTE=${REMOTE:-https://github.com/${REPO}.git} BRANCH=${BRANCH:-master} - command_exists() { command -v "$@" >/dev/null 2>&1 } @@ -76,7 +77,7 @@ setup_ohmyzsh() { exit 1 fi - git clone --depth=1 --branch "$BRANCH" "https://github.com/$REPO.git" "$ZSH" || { + git clone --depth=1 --branch "$BRANCH" "$REMOTE" "$ZSH" || { error "git clone of oh-my-zsh repo failed" exit 1 } From 153f5e11ed2f2cd50afe8804ecf1c2d6ef8e1f35 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Cornell=C3=A0?= Date: Fri, 24 May 2019 19:58:09 +0200 Subject: [PATCH 14/25] installer: use default color sequences on missing tput Supposed to be POSIX-compatible. Proved to work in dash, yash and whatever alpine uses. See https://unix.stackexchange.com/a/371873 --- tools/install.sh | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/tools/install.sh b/tools/install.sh index db23d9896..e69ce4b83 100755 --- a/tools/install.sh +++ b/tools/install.sh @@ -27,7 +27,7 @@ command_exists() { } error() { - echo "Error: $@" >&2 + echo ${RED}"Error: $@"${RESET} >&2 } # Set up color sequences @@ -45,14 +45,14 @@ setup_color() { YELLOW="$(tput setaf 3)" BLUE="$(tput setaf 4)" BOLD="$(tput bold)" - NORMAL="$(tput sgr0)" + RESET="$(tput sgr0)" else - RED="" - GREEN="" - YELLOW="" - BLUE="" - BOLD="" - NORMAL="" + RED=$(printf '\033[31m') + GREEN=$(printf '\033[32m') + YELLOW=$(printf '\033[33m') + BLUE=$(printf '\033[34m') + BOLD=$(printf '\033[1m') + RESET=$(printf '\033[m') fi } @@ -64,7 +64,7 @@ setup_ohmyzsh() { # precedence over umasks except for filesystems mounted with option "noacl". umask g-w,o-w - echo "${BLUE}Cloning Oh My Zsh...${NORMAL}" + echo "${BLUE}Cloning Oh My Zsh...${RESET}" command_exists git || { error "git is not installed" @@ -84,13 +84,13 @@ setup_ohmyzsh() { } setup_zshrc() { - echo "${BLUE}Looking for an existing zsh config...${NORMAL}" + echo "${BLUE}Looking for an existing zsh config...${RESET}" if [ -f ~/.zshrc ] || [ -h ~/.zshrc ]; then - echo "${YELLOW}Found ~/.zshrc.${GREEN} Backing up to ~/.zshrc.pre-oh-my-zsh.${NORMAL}" + echo "${YELLOW}Found ~/.zshrc.${GREEN} Backing up to ~/.zshrc.pre-oh-my-zsh.${RESET}" mv ~/.zshrc ~/.zshrc.pre-oh-my-zsh fi - echo "${BLUE}Using the Oh My Zsh template file and adding it to ~/.zshrc.${NORMAL}" + echo "${BLUE}Using the Oh My Zsh template file and adding it to ~/.zshrc.${RESET}" cp "$ZSH/templates/zshrc.zsh-template" ~/.zshrc sed "/^export ZSH=/ c\\ @@ -109,12 +109,12 @@ setup_shell() { if ! command_exists chsh; then cat <<-EOF I can't change your shell automatically because this system does not have chsh. - ${BLUE}Please manually change your default shell to zsh${NORMAL} + ${BLUE}Please manually change your default shell to zsh${RESET} EOF return fi - echo "${BLUE}Time to change your default shell to zsh!${NORMAL}" + echo "${BLUE}Time to change your default shell to zsh!${RESET}" # Test for the right location of the "shells" file if [ -f /etc/shells ]; then @@ -146,13 +146,13 @@ main() { setup_color if ! command_exists zsh; then - echo "${YELLOW}Zsh is not installed.${NORMAL} Please install zsh first." + echo "${YELLOW}Zsh is not installed.${RESET} Please install zsh first." exit 1 fi if [ -d "$ZSH" ]; then cat <<-EOF - ${YELLOW}You already have Oh My Zsh installed.${NORMAL} + ${YELLOW}You already have Oh My Zsh installed.${RESET} You'll need to remove '$ZSH' if you want to reinstall. EOF exit 1 @@ -179,7 +179,7 @@ main() { p.p.s. Get stickers, shirts, and coffee mugs at https://shop.planetargon.com/collections/oh-my-zsh EOF - printf "$NORMAL" + printf "$RESET" exec zsh -l } From 43b3126b5cd2253330edf73930d37f17eaf4328f Mon Sep 17 00:00:00 2001 From: Andrew Janke Date: Wed, 23 Sep 2015 18:41:48 -0400 Subject: [PATCH 15/25] installer: use timestamped backups to preserve all old zshrcs --- tools/install.sh | 22 ++++++++++++++++++++-- tools/uninstall.sh | 15 ++++++++------- 2 files changed, 28 insertions(+), 9 deletions(-) diff --git a/tools/install.sh b/tools/install.sh index e69ce4b83..66da05690 100755 --- a/tools/install.sh +++ b/tools/install.sh @@ -84,10 +84,28 @@ setup_ohmyzsh() { } setup_zshrc() { + # Keep most recent old .zshrc at .zshrc.pre-oh-my-zsh, and older ones + # with datestamp of installation that moved them aside, so we never actually + # destroy a user's original zshrc echo "${BLUE}Looking for an existing zsh config...${RESET}" + + # Must use this exact name so uninstall.sh can find it + OLD_ZSHRC=~/.zshrc.pre-oh-my-zsh if [ -f ~/.zshrc ] || [ -h ~/.zshrc ]; then - echo "${YELLOW}Found ~/.zshrc.${GREEN} Backing up to ~/.zshrc.pre-oh-my-zsh.${RESET}" - mv ~/.zshrc ~/.zshrc.pre-oh-my-zsh + if [ -e "$OLD_ZSHRC" ]; then + OLD_OLD_ZSHRC="${OLD_ZSHRC}-$(date +%Y-%m-%d_%H-%M-%S)" + if [ -e "$OLD_OLD_ZSHRC" ]; then + error "$OLD_OLD_ZSHRC exists. Can't back up ${OLD_ZSHRC}" + error "re-run the installer again in a couple of seconds" + exit 1 + fi + mv "$OLD_ZSHRC" "${OLD_OLD_ZSHRC}" + + echo "${YELLOW}Found old ~/.zshrc.pre-oh-my-zsh." \ + "${GREEN}Backing up to ${OLD_OLD_ZSHRC}${RESET}" + fi + echo "${YELLOW}Found ~/.zshrc.${RESET} ${GREEN}Backing up to ${OLD_ZSHRC}${RESET}" + mv ~/.zshrc "$OLD_ZSHRC" fi echo "${BLUE}Using the Oh My Zsh template file and adding it to ~/.zshrc.${RESET}" diff --git a/tools/uninstall.sh b/tools/uninstall.sh index bf2244be8..9793be2a1 100644 --- a/tools/uninstall.sh +++ b/tools/uninstall.sh @@ -10,16 +10,17 @@ if [ -d ~/.oh-my-zsh ]; then fi echo "Looking for original zsh config..." -if [ -f ~/.zshrc.pre-oh-my-zsh ] || [ -h ~/.zshrc.pre-oh-my-zsh ]; then - echo "Found ~/.zshrc.pre-oh-my-zsh -- Restoring to ~/.zshrc"; +ZSHRC_ORIG=~/.zshrc.pre-oh-my-zsh +if [ -e "$ZSHRC_ORIG" ]; then + echo "Found $ZSHRC_ORIG -- Restoring to ~/.zshrc" - if [ -f ~/.zshrc ] || [ -h ~/.zshrc ]; then - ZSHRC_SAVE=".zshrc.omz-uninstalled-$(date +%Y%m%d%H%M%S)"; - echo "Found ~/.zshrc -- Renaming to ~/${ZSHRC_SAVE}"; - mv ~/.zshrc ~/"${ZSHRC_SAVE}"; + if [ -e ~/.zshrc ]; then + ZSHRC_SAVE=~/.zshrc.omz-uninstalled-$(date +%Y-%m-%d_%H-%M-%S) + echo "Found ~/.zshrc -- Renaming to ${ZSHRC_SAVE}" + mv ~/.zshrc "${ZSHRC_SAVE}" fi - mv ~/.zshrc.pre-oh-my-zsh ~/.zshrc; + mv "$ZSHRC_ORIG" ~/.zshrc echo "Your original zsh config was restored. Please restart your session." else From 794ff4a62daa57b985ea35a3d6dc879d771e53f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Cornell=C3=A0?= Date: Mon, 27 May 2019 20:41:55 +0200 Subject: [PATCH 16/25] installer: add ability to skip the default shell change Co-authored-by: Marshall Ford Co-authored-by: Joel Kuzmarski --- tools/install.sh | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/tools/install.sh b/tools/install.sh index 66da05690..b479dc803 100755 --- a/tools/install.sh +++ b/tools/install.sh @@ -10,9 +10,12 @@ # sh install.sh # # Respects these environment variables for tweaking the installation process: +# ZSH - path to the Oh My Zsh repository folder (default: $HOME/.oh-my-zsh) # REPO - name of the GitHub repo to install from (default: robbyrussell/oh-my-zsh) # REMOTE - full remote URL of the git repo to install (default: GitHub via HTTPS) # BRANCH - branch to check out immediately after install (default: master) +# Other options: +# CHSH - set to no tells the installer not to change the default shell (default: yes) # set -e @@ -22,6 +25,10 @@ REPO=${REPO:-robbyrussell/oh-my-zsh} REMOTE=${REMOTE:-https://github.com/${REPO}.git} BRANCH=${BRANCH:-master} +# Other options +CHSH=${CHSH:-yes} + + command_exists() { command -v "$@" >/dev/null 2>&1 } @@ -118,6 +125,11 @@ export ZSH=\"$ZSH\" } setup_shell() { + # Skip setup if the user wants or stdin is closed (not running interactively). + if [ $CHSH = no ] || ! [ -t 0 ]; then + return + fi + # If this user's login shell is already "zsh", do not attempt to switch. if [ "$(basename "$SHELL")" = "zsh" ]; then return @@ -161,6 +173,14 @@ setup_shell() { } main() { + # Parse arguments + while [ $# -gt 0 ]; do + case $1 in + --skip-chsh) CHSH=no ;; + esac + shift + done + setup_color if ! command_exists zsh; then @@ -202,4 +222,4 @@ main() { exec zsh -l } -main +main "$@" From a40d93295f91d1553522e58433e0ba2c4186d2b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Cornell=C3=A0?= Date: Mon, 27 May 2019 22:21:39 +0200 Subject: [PATCH 17/25] installer: don't run zsh at the end Co-authored-by: Joel Kuzmarski --- tools/install.sh | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tools/install.sh b/tools/install.sh index b479dc803..2d2936531 100755 --- a/tools/install.sh +++ b/tools/install.sh @@ -219,6 +219,11 @@ main() { EOF printf "$RESET" + if [ ! -t 0 ]; then + echo "${YELLOW}Run zsh to try it out.${RESET}" + exit + fi + exec zsh -l } From 1cf42a506ff55d12c3246239332469d193bbf4b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Cornell=C3=A0?= Date: Mon, 27 May 2019 22:58:33 +0200 Subject: [PATCH 18/25] installer: add option to not run zsh at the end Co-authored-by: Liquidsoul Co-authored-by: Alexander Polynomdivision Co-authored-by: loket Co-authored-by: Connor Demille --- tools/install.sh | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/tools/install.sh b/tools/install.sh index 2d2936531..9eb83cfbd 100755 --- a/tools/install.sh +++ b/tools/install.sh @@ -15,7 +15,8 @@ # REMOTE - full remote URL of the git repo to install (default: GitHub via HTTPS) # BRANCH - branch to check out immediately after install (default: master) # Other options: -# CHSH - set to no tells the installer not to change the default shell (default: yes) +# CHSH - 'no' means the installer will not change the default shell (default: yes) +# RUNZSH - 'no' means the installer will not run zsh after the install (default: yes) # set -e @@ -27,6 +28,7 @@ BRANCH=${BRANCH:-master} # Other options CHSH=${CHSH:-yes} +RUNZSH=${RUNZSH:-yes} command_exists() { @@ -126,7 +128,7 @@ export ZSH=\"$ZSH\" setup_shell() { # Skip setup if the user wants or stdin is closed (not running interactively). - if [ $CHSH = no ] || ! [ -t 0 ]; then + if [ $CHSH = no ]; then return fi @@ -173,9 +175,15 @@ setup_shell() { } main() { + if [ ! -t 0 ]; then + RUNZSH=no + CHSH=no + fi + # Parse arguments while [ $# -gt 0 ]; do case $1 in + --silent|--batch) RUNZSH=no; CHSH=no ;; --skip-chsh) CHSH=no ;; esac shift @@ -219,7 +227,7 @@ main() { EOF printf "$RESET" - if [ ! -t 0 ]; then + if [ $RUNZSH = no ]; then echo "${YELLOW}Run zsh to try it out.${RESET}" exit fi From 065844fdc058360a66320f9d0e6a04fc12853a05 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Cornell=C3=A0?= Date: Mon, 27 May 2019 22:58:58 +0200 Subject: [PATCH 19/25] installer: comment changes --- tools/install.sh | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tools/install.sh b/tools/install.sh index 9eb83cfbd..05b485333 100755 --- a/tools/install.sh +++ b/tools/install.sh @@ -9,7 +9,10 @@ # wget https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh # sh install.sh # -# Respects these environment variables for tweaking the installation process: +# You can tweak the install behavior by setting variables when running the script. For +# example, to change the path to the Oh My Zsh repository: +# ZSH=~/.zsh sh install.sh +# Respects the following environment variables: # ZSH - path to the Oh My Zsh repository folder (default: $HOME/.oh-my-zsh) # REPO - name of the GitHub repo to install from (default: robbyrussell/oh-my-zsh) # REMOTE - full remote URL of the git repo to install (default: GitHub via HTTPS) From b931d6a9dbab9e507179ae405455e443102fcc28 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Cornell=C3=A0?= Date: Tue, 28 May 2019 17:34:05 +0200 Subject: [PATCH 20/25] installer: change to --unattended argument and add docs --- tools/install.sh | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/tools/install.sh b/tools/install.sh index 05b485333..8763b3513 100755 --- a/tools/install.sh +++ b/tools/install.sh @@ -12,15 +12,23 @@ # You can tweak the install behavior by setting variables when running the script. For # example, to change the path to the Oh My Zsh repository: # ZSH=~/.zsh sh install.sh +# # Respects the following environment variables: # ZSH - path to the Oh My Zsh repository folder (default: $HOME/.oh-my-zsh) # REPO - name of the GitHub repo to install from (default: robbyrussell/oh-my-zsh) # REMOTE - full remote URL of the git repo to install (default: GitHub via HTTPS) # BRANCH - branch to check out immediately after install (default: master) +# # Other options: # CHSH - 'no' means the installer will not change the default shell (default: yes) # RUNZSH - 'no' means the installer will not run zsh after the install (default: yes) # +# You can also pass some arguments to the install script to set some these options: +# --skip-chsh: has the same behavior as setting CHSH to 'no' +# --unattended: sets both CHSH and RUNZSH to 'no' +# For example: +# sh install.sh --unattended +# set -e # Default settings @@ -178,6 +186,7 @@ setup_shell() { } main() { + # Run as unattended if stdin is closed if [ ! -t 0 ]; then RUNZSH=no CHSH=no @@ -186,7 +195,7 @@ main() { # Parse arguments while [ $# -gt 0 ]; do case $1 in - --silent|--batch) RUNZSH=no; CHSH=no ;; + --unattended) RUNZSH=no; CHSH=no ;; --skip-chsh) CHSH=no ;; esac shift From 66d6d08841ebde3610eaee961f63f8420e914d3b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Cornell=C3=A0?= Date: Tue, 28 May 2019 18:34:58 +0200 Subject: [PATCH 21/25] installer: improve message formatting and color --- tools/install.sh | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/tools/install.sh b/tools/install.sh index 8763b3513..3f0e468fe 100755 --- a/tools/install.sh +++ b/tools/install.sh @@ -101,6 +101,8 @@ setup_ohmyzsh() { error "git clone of oh-my-zsh repo failed" exit 1 } + + echo } setup_zshrc() { @@ -128,13 +130,15 @@ setup_zshrc() { mv ~/.zshrc "$OLD_ZSHRC" fi - echo "${BLUE}Using the Oh My Zsh template file and adding it to ~/.zshrc.${RESET}" + echo "${GREEN}Using the Oh My Zsh template file and adding it to ~/.zshrc.${RESET}" cp "$ZSH/templates/zshrc.zsh-template" ~/.zshrc sed "/^export ZSH=/ c\\ export ZSH=\"$ZSH\" " ~/.zshrc > ~/.zshrc-omztemp mv -f ~/.zshrc-omztemp ~/.zshrc + + echo } setup_shell() { @@ -157,7 +161,7 @@ setup_shell() { return fi - echo "${BLUE}Time to change your default shell to zsh!${RESET}" + echo "${BLUE}Time to change your default shell to zsh:${RESET}" # Test for the right location of the "shells" file if [ -f /etc/shells ]; then @@ -174,7 +178,8 @@ setup_shell() { # 2. If that fails, get a zsh path from the shells file, then check it actually exists if ! zsh=$(which zsh) || ! grep -qx "$zsh" "$shells_file"; then if ! zsh=$(grep '^/.*/zsh$' "$shells_file" | tail -1) || [ ! -f "$zsh" ]; then - error "no available zsh binary found. Change your default shell manually." + error "no zsh binary found or not present in '$shells_file'" + error "change your default shell manually." return fi fi @@ -182,7 +187,12 @@ setup_shell() { # Actually change the default shell to zsh if ! chsh -s "$zsh"; then error "chsh command unsuccessful. Change your default shell manually." + else + export SHELL="$zsh" + echo "${GREEN}Shell successfully changed to '$zsh'.${RESET}" fi + + echo } main() { From 2e54ba2dfb95890c6ea32323ae14486ffc1c46ed Mon Sep 17 00:00:00 2001 From: Zach Whitten Date: Fri, 13 Oct 2017 14:33:22 -0400 Subject: [PATCH 22/25] installer: restore previous default shell with uninstall MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Antonio QUINTAVALLE Co-authored-by: Marc Cornellà --- tools/install.sh | 7 +++++++ tools/uninstall.sh | 21 +++++++++++++++------ 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/tools/install.sh b/tools/install.sh index 3f0e468fe..15c7b7703 100755 --- a/tools/install.sh +++ b/tools/install.sh @@ -184,6 +184,13 @@ setup_shell() { fi fi + # We're going to change the default shell, so back up the current one + if [ -n $SHELL ]; then + echo $SHELL > ~/.shell.pre-oh-my-zsh + else + grep "^$USER:" /etc/passwd | awk -F: '{print $7}' > ~/.shell.pre-oh-my-zsh + fi + # Actually change the default shell to zsh if ! chsh -s "$zsh"; then error "chsh command unsuccessful. Change your default shell manually." diff --git a/tools/uninstall.sh b/tools/uninstall.sh index 9793be2a1..da31a6a14 100644 --- a/tools/uninstall.sh +++ b/tools/uninstall.sh @@ -22,14 +22,23 @@ if [ -e "$ZSHRC_ORIG" ]; then mv "$ZSHRC_ORIG" ~/.zshrc - echo "Your original zsh config was restored. Please restart your session." -else - if hash chsh >/dev/null 2>&1; then - echo "Switching back to bash" - chsh -s /bin/bash + echo "Your original zsh config was restored." +fi + +if hash chsh >/dev/null 2>&1; then + if [ -f ~/.shell.pre-oh-my-zsh ]; then + old_shell=$(cat ~/.shell.pre-oh-my-zsh) else - echo "You can edit /etc/passwd to switch your default shell back to bash" + old_shell=/bin/bash + fi + echo "Switching your shell back to '$old_shell':" + if chsh -s "$old_shell"; then + rm -f ~/.shell.pre-oh-my-zsh + else + echo "Could not change default shell. Change it manually by running chsh" + echo "or editing the /etc/passwd file." fi fi echo "Thanks for trying out Oh My Zsh. It's been uninstalled." +echo "Don't forget to restart your terminal!" From 0824dcc9fbd66171f53c38c8327598ec469ebe85 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Cornell=C3=A0?= Date: Sat, 1 Jun 2019 19:26:17 +0200 Subject: [PATCH 23/25] installer: allow for tput errors tput may throw errors on invalid $TERM values, for example. This shorthand syntax allows for that as well as for if tput doesn't exist. --- tools/install.sh | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/tools/install.sh b/tools/install.sh index 15c7b7703..a515bac48 100755 --- a/tools/install.sh +++ b/tools/install.sh @@ -52,11 +52,7 @@ error() { # Set up color sequences setup_color() { - if command_exists tput; then - ncolors=$(tput colors) - else - ncolors=0 - fi + ncolors=$(tput colors 2>/dev/null) || ncolors=0 # Only use colors if connected to a terminal that supports them if [ -t 1 ] && [ $ncolors -ge 8 ]; then From b944fee6ea0b5616e8bff95206c3ce57d268625f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Cornell=C3=A0?= Date: Sat, 1 Jun 2019 21:12:08 +0200 Subject: [PATCH 24/25] installer: ask user about changing the shell to zsh --- tools/install.sh | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tools/install.sh b/tools/install.sh index a515bac48..d56d8addc 100755 --- a/tools/install.sh +++ b/tools/install.sh @@ -159,6 +159,15 @@ setup_shell() { echo "${BLUE}Time to change your default shell to zsh:${RESET}" + # Prompt for user choice on changing the default login shell + printf "${YELLOW}Do you want to change your default shell to zsh? [Y/n]${RESET} " + read opt + case $opt in + y*|Y*|"") echo "Changing the shell..." ;; + n*|N*) echo "Shell change skipped."; return ;; + *) echo "Invalid choice. Shell change skipped."; return ;; + esac + # Test for the right location of the "shells" file if [ -f /etc/shells ]; then shells_file=/etc/shells From c8ac4038cf41112161de984d3f7eef8579eb085e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Cornell=C3=A0?= Date: Mon, 3 Jun 2019 16:47:54 +0200 Subject: [PATCH 25/25] installer: add documentation of new installer features to README --- README.md | 69 ++++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 61 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 38f6ae9f3..3924c3a92 100644 --- a/README.md +++ b/README.md @@ -38,7 +38,18 @@ sh -c "$(curl -fsSL https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/mas #### via wget ```shell -sh -c "$(wget https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh -O -)" +sh -c "$(wget -O- https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh)" +``` + +#### Manual inspection + +It's a good idea to inspect the install script from projects you don't yet know. You can do +that by downloading the install script first, looking through it so everything looks normal, +then running it: + +```shell +curl -Lo install.sh https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh +sh install.sh ``` ## Using Oh My Zsh @@ -69,6 +80,8 @@ plugins=( ) ``` +_Note that the plugins are separated by whitespace. **Do not** use commas between them._ + #### Using Plugins Most plugins (should! we're working on this) include a __README__, which documents how to use them. @@ -124,16 +137,53 @@ If you're the type that likes to get their hands dirty, these sections might res ### Advanced Installation -Some users may want to change the default path, or manually install Oh My Zsh. +Some users may want to manually install Oh My Zsh, or change the default path or other settings that +the installer accepts (these settings are also documented at the top of the install script). #### Custom Directory The default location is `~/.oh-my-zsh` (hidden in your home directory) -If you'd like to change the install directory with the `ZSH` environment variable, either by running `export ZSH=/your/path` before installing, or by setting it before the end of the install pipeline like this: +If you'd like to change the install directory with the `ZSH` environment variable, either by running +`export ZSH=/your/path` before installing, or by setting it before the end of the install pipeline +like this: ```shell -export ZSH="$HOME/.dotfiles/oh-my-zsh"; sh -c "$(curl -fsSL https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh)" +ZSH="$HOME/.dotfiles/oh-my-zsh" sh install.sh +``` + +#### Unattended install + +If you're running the Oh My Zsh install script as part of an automated install, you can pass the +flag `--unattended` to the `install.sh` script. This will have the effect of not trying to change +the default shell, and also won't run `zsh` when the installation has finished. + +```shell +curl -Lo install.sh https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh +sh install.sh --unattended +``` + +#### Installing from a forked repository + +The install script also accepts these variables to allow installation of a different repository: + +- `REPO` (default: `robbyrussell/oh-my-zsh`): this takes the form of `owner/repository`. If you set + this variable, the installer will look for a repository at `https://github.com/{owner}/{repository}`. + +- `REMOTE` (default: `https://github.com/${REPO}.git`): this is the full URL of the git repository + clone. You can use this setting if you want to install from a fork that is not on GitHub (GitLab, + Bitbucket...) or if you want to clone with SSH instead of HTTPS (`git@github.com:user/project.git`). + + _NOTE: it's incompatible with setting the `REPO` variable. This setting will take precedence._ + +- `BRANCH` (default: `master`): you can use this setting if you want to change the default branch to be + checked out when cloning the repository. This might be useful for testing a Pull Request, or if you + want to use a branch other than `master`. + +For example: + +```shell +REPO=apjanke/oh-my-zsh BRANCH=edge sh install.sh ``` #### Manual Installation @@ -161,9 +211,10 @@ cp ~/.oh-my-zsh/templates/zshrc.zsh-template ~/.zshrc ##### 4. Change your default shell ```shell -chsh -s /bin/zsh +chsh -s $(which zsh) ``` -You must log out and log back in to see this change. + +You must log out from your user session and log back in to see this change. ##### 5. Initialize your new zsh configuration @@ -173,8 +224,10 @@ Once you open up a new terminal window, it should load zsh with Oh My Zsh's conf If you have any hiccups installing, here are a few common fixes. -* You _might_ need to modify your `PATH` in `~/.zshrc` if you're not able to find some commands after switching to `oh-my-zsh`. -* If you installed manually or changed the install location, check the `ZSH` environment variable in `~/.zshrc`. +* You _might_ need to modify your `PATH` in `~/.zshrc` if you're not able to find some commands after +switching to `oh-my-zsh`. +* If you installed manually or changed the install location, check the `ZSH` environment variable in +`~/.zshrc`. ### Custom Plugins and Themes