From 11507c3f386553f696366cb5ecf251a16c89b512 Mon Sep 17 00:00:00 2001 From: Michael Martinez Date: Thu, 15 Jan 2015 01:35:36 -0800 Subject: [PATCH] Fixes bug where NVM init script is appended to the wrong startup file The existence of .bashrc, .bash_profile or .zshrc doesn't prove that the file is the currently utilized startup file or that it is the only file sourced. Because the current code operates under this assumption, the code: ```bash export NVM_DIR="/Users//.nvm" [ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" # This loads nvm ``` is getting appended to the wrong profile for zsh users. 1) `[ -d ~/.nvm ] && rm -rfv ~/.nvm ` 2) Install Zsh via https://github.com/robbyrussell/oh-my-zsh 3) Run nvm install script with cURL. 4) Run `which nvm`. Observe it's not in `$PATH` 5) Open a new shell session. Observe the issue persists. To fix this, the code in this PR checks: - Is `$PROFILE` set? If so, echo it. - What is the active shell that is executing this script? (the parent shell) - If the shell is zsh, echo the zshrc file - If the shell is bash: - Is it login or non-login? Echo the neccessary file - Is it interactive or non interactive? Echo the neccessary file. This logic should address the primary bug and also prevent future bugs. Note: bash by default reads ~/.profile last so it makes sense to write to ~/.bashrc or ~/.bash_profile instead of ~/.profile See: https://www.gnu.org/software/bash/manual/html_node/Bash-Startup-Files.html Passed Manual Tests: :white_check_mark: zsh :white_check_mark: bash from within script :white_check_mark: bash as default shell :white_check_mark: bash -l from within zsh --- install.sh | 33 ++++++++++++++++++++------------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/install.sh b/install.sh index e64d552..d7d553a 100755 --- a/install.sh +++ b/install.sh @@ -100,26 +100,33 @@ install_nvm_as_script() { } } -# # Detect profile file if not specified as environment variable # (eg: PROFILE=~/.myprofile) # The echo'ed path is guaranteed to be an existing file # Otherwise, an empty string is returned -# +# +# Auto detects the appropriate shell by looking at the shell of +# the caller process and login/interactive status. nvm_detect_profile() { - if [ -f "$PROFILE" ]; then - echo "$PROFILE" - elif [ -f "$HOME/.bashrc" ]; then - echo "$HOME/.bashrc" - elif [ -f "$HOME/.bash_profile" ]; then - echo "$HOME/.bash_profile" - elif [ -f "$HOME/.zshrc" ]; then - echo "$HOME/.zshrc" - elif [ -f "$HOME/.profile" ]; then - echo "$HOME/.profile" - fi + local shell=$(ps $PPID | tail -n 1 | awk '{print $(NF-0)}') + if [ -f "$PROFILE" ]; then + echo "$PROFILE" + elif echo $shell | grep -q "zsh"; then + echo "$HOME/.zshrc" + elif echo $shell | grep -q "bash"; then + local login_status=$(shopt -q login_shell) + local interactive_status=$([[ $- == *i* ]]) + if [[ $login_status -eq 0 ]]; then + echo "$HOME/.bashrc" + elif [[ $interactive_status -eq 0 ]]; then + echo "$HOME/.bash_profile" + fi + else + echo "" + fi } + nvm_do_install() { if [ -z "$METHOD" ]; then # Autodetect install method