From d01b62b5940a36ad32c28937f95f67b82862b884 Mon Sep 17 00:00:00 2001 From: Jim Heald Date: Sat, 18 Jul 2020 11:11:41 -0700 Subject: [PATCH] [Fix] `install`: detect user shell and try shellrc file first --- install.sh | 13 +++++++++- test/install_script/nvm_detect_profile | 26 ++++++++++++++----- .../nvm_install_with_aliased_dot | 2 +- 3 files changed, 33 insertions(+), 8 deletions(-) diff --git a/install.sh b/install.sh index 0ea9a0a..99f69a6 100755 --- a/install.sh +++ b/install.sh @@ -233,7 +233,18 @@ nvm_detect_profile() { local DETECTED_PROFILE DETECTED_PROFILE='' - if [ -n "${BASH_VERSION-}" ]; then + # Detect the user's login shell + local DETECTED_SHELL + local SHELLRC + + DETECTED_SHELL="${SHELL##*/}" + SHELLRC="$HOME/.${DETECTED_SHELL}rc" + + if [ -n "${DETECTED_SHELL}" ]; then + if [ -f "$SHELLRC" ]; then + DETECTED_PROFILE="$SHELLRC" + fi + elif [ -n "${BASH_VERSION-}" ]; then if [ -f "$HOME/.bashrc" ]; then DETECTED_PROFILE="$HOME/.bashrc" elif [ -f "$HOME/.bash_profile" ]; then diff --git a/test/install_script/nvm_detect_profile b/test/install_script/nvm_detect_profile index 54815ec..02339ef 100755 --- a/test/install_script/nvm_detect_profile +++ b/test/install_script/nvm_detect_profile @@ -2,6 +2,7 @@ setup () { HOME="." + SHELL='/bin/bash' NVM_ENV=testing \. ../../install.sh touch ".bashrc" touch ".bash_profile" @@ -12,6 +13,7 @@ setup () { cleanup () { unset HOME + unset SHELL unset NVM_ENV unset NVM_DETECT_PROFILE unset BASH_VERSION @@ -34,6 +36,12 @@ if [ -n "$NVM_DETECT_PROFILE" ]; then die "nvm_detect_profile still detected a profile even though PROFILE=/dev/null" fi +# .bashrc should be detected if the shell is bash +NVM_DETECT_PROFILE="$(unset PROFILE; nvm_detect_profile)" +if [ "$NVM_DETECT_PROFILE" != "$HOME/.bashrc" ]; then + die "nvm_detect_profile didn't pick \$HOME/.bashrc for bash" +fi + # .bashrc should be detected for bash NVM_DETECT_PROFILE="$(BASH_VERSION="1"; unset PROFILE; nvm_detect_profile)" if [ "$NVM_DETECT_PROFILE" != "$HOME/.bashrc" ]; then @@ -46,8 +54,14 @@ if [ "$NVM_DETECT_PROFILE" != "test_profile" ]; then die "nvm_detect_profile ignored \$PROFILE" fi +# .zshrc should be detected if the shell is zsh +NVM_DETECT_PROFILE="$(SHELL="/bin/zsh"; unset PROFILE; unset BASH_VERSION; nvm_detect_profile)" +if [ "$NVM_DETECT_PROFILE" != "$HOME/.zshrc" ]; then + die "nvm_detect_profile didn't pick \$HOME/.zshrc for zsh" +fi + # .zshrc should be detected for zsh -NVM_DETECT_PROFILE="$(ZSH_VERSION="1"; unset PROFILE; unset BASH_VERSION; nvm_detect_profile)" +NVM_DETECT_PROFILE="$(SHELL="/bin/zsh"; ZSH_VERSION="1"; unset PROFILE; unset BASH_VERSION; nvm_detect_profile)" if [ "$NVM_DETECT_PROFILE" != "$HOME/.zshrc" ]; then die "nvm_detect_profile didn't pick \$HOME/.zshrc for zsh" fi @@ -82,29 +96,29 @@ fi # return an empty value if everything fails # -# It should favor .profile if file exists -NVM_DETECT_PROFILE="$(unset BASH_VERSION; unset ZSH_VERSION; nvm_detect_profile)" +# It should favor .profile if other detection methods fail and file exists and +NVM_DETECT_PROFILE="$(unset BASH_VERSION; unset ZSH_VERSION; unset SHELL; nvm_detect_profile)" if [ "$NVM_DETECT_PROFILE" != "$HOME/.profile" ]; then die "nvm_detect_profile should have selected .profile" fi # Otherwise, it should favor .bashrc if file exists rm ".profile" -NVM_DETECT_PROFILE="$(unset BASH_VERSION; unset ZSH_VERSION; nvm_detect_profile)" +NVM_DETECT_PROFILE="$(unset BASH_VERSION; unset ZSH_VERSION; unset SHELL; nvm_detect_profile)" if [ "$NVM_DETECT_PROFILE" != "$HOME/.bashrc" ]; then die "nvm_detect_profile should have selected .bashrc" fi # Otherwise, it should favor .bash_profile if file exists rm ".bashrc" -NVM_DETECT_PROFILE="$(unset BASH_VERSION; unset ZSH_VERSION; nvm_detect_profile)" +NVM_DETECT_PROFILE="$(unset BASH_VERSION; unset ZSH_VERSION; unset SHELL; nvm_detect_profile)" if [ "$NVM_DETECT_PROFILE" != "$HOME/.bash_profile" ]; then die "nvm_detect_profile should have selected .bash_profile" fi # Otherwise, it should favor .zshrc if file exists rm ".bash_profile" -NVM_DETECT_PROFILE="$(unset BASH_VERSION; unset ZSH_VERSION; nvm_detect_profile)" +NVM_DETECT_PROFILE="$(unset BASH_VERSION; unset ZSH_VERSION; unset SHELL; nvm_detect_profile)" if [ "$NVM_DETECT_PROFILE" != "$HOME/.zshrc" ]; then die "nvm_detect_profile should have selected .zshrc" fi diff --git a/test/install_script/nvm_install_with_aliased_dot b/test/install_script/nvm_install_with_aliased_dot index b8f05a5..d3f396f 100755 --- a/test/install_script/nvm_install_with_aliased_dot +++ b/test/install_script/nvm_install_with_aliased_dot @@ -1,4 +1,4 @@ -#!/bin/sh +#!/bin/bash setup () { shopt -s expand_aliases