Skip to content

[Fix] install.sh shouldn't use not existed variable - #1605

Merged
ljharb merged 1 commit into
nvm-sh:masterfrom
PeterDaveHello:fix-install.sh
Oct 4, 2017
Merged

ljharb merged 1 commit into
nvm-sh:masterfrom
PeterDaveHello:fix-install.sh

Conversation

@PeterDaveHello

Copy link
Copy Markdown
Collaborator

Original codes will print like:

=> Downloading nvm as script to '/root/.nvm'

=> Profile not found. Tried  (as defined in $PROFILE), ~/.bashrc, ~/.bash_profile, ~/.zshrc, and ~/.profile.
=> Create one of them and run this script again
=> Create it (touch ) and run this script again
   OR
=> Append the following lines to the correct file yourself:

Comment thread install.sh
BASH_OR_ZSH=false

if [ -z "${NVM_PROFILE-}" ] ; then
echo "=> Profile not found. Tried ${NVM_PROFILE} (as defined in \$PROFILE), ~/.bashrc, ~/.bash_profile, ~/.zshrc, and ~/.profile."

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm, couldn't $PROFILE define a file that doesn't exist?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just move it to the end of the sentence, didn't remove it, would that be good?

The commit message was wrong, the point here is the installation script use not defined ${NVM_PROFILE} :)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Totally, but it seems like NVM_PROFILE will only be undefined some of the time; which means that some of the time, displaying it is very helpful.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmmmm ... I don't understand, there is a line if [ -z "${NVM_PROFILE-}" ] ; then before, doesn't that mean it's always not defined in this code block?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right - $NVM_PROFILE will of course always be empty in this block; but the purpose of the message is to indicate the exact value the user had defined in their profile file. nvm_detect_profile looks at $PROFILE - I assume that's the original purpose of this message?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not very sure, so is this message going to tell the users it has tried ${NVM_PROFILE} this variable? Or just tell them that it has tried the following files?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that if a profile file defines $PROFILE, but the -f check fails, then the error message should display the value of $PROFILE and indicate that it doesn't exist.

@ljharb ljharb added the installing nvm Problems installing nvm itself label Sep 8, 2017
@PeterDaveHello PeterDaveHello changed the title [Fix] install.sh shouldn't use not existed [Fix] install.sh shouldn't use not existed variable Sep 8, 2017
@PeterDaveHello

Copy link
Copy Markdown
Collaborator Author

@ljharb I think I improved it with success, please take a look, thanks.

Comment thread install.sh
fi
echo "=> Profile not found. Tried ${TRIED_PROFILE-} ~/.bashrc, ~/.bash_profile, ~/.zshrc, and ~/.profile."
echo "=> Create one of them and run this script again"
echo "=> Create it (touch ${NVM_PROFILE}) and run this script again"

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line seems to be duplicated, I guess we can simply remove it.

Comment thread install.sh Outdated
if [ -z "${NVM_PROFILE-}" ] ; then
echo "=> Profile not found. Tried ${NVM_PROFILE} (as defined in \$PROFILE), ~/.bashrc, ~/.bash_profile, ~/.zshrc, and ~/.profile."
if [ -n "${PROFILE}" ]; then
local TRIED_PROFILE

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

doesn't this local have to appear outside the if, if it's referenced outside it?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's still in the same function?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For clarity, even if it's not required, it'd be nice to declare the var at a high enough scope to eclipse all uses.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So would moving it to above current line 333 be okay?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yup, seems good!

Comment thread install.sh Outdated
local TRIED_PROFILE
TRIED_PROFILE="${NVM_PROFILE} (as defined in \$PROFILE), "
fi
echo "=> Profile not found. Tried ${TRIED_PROFILE-} ~/.bashrc, ~/.bash_profile, ~/.zshrc, and ~/.profile."

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like it adds an extra space after TRIED_PROFILE?

@PeterDaveHello

Copy link
Copy Markdown
Collaborator Author

All fixed @ljharb

@ljharb ljharb left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM with one tweak

Comment thread install.sh Outdated
if [ -n "${PROFILE}" ]; then
TRIED_PROFILE="${NVM_PROFILE} (as defined in \$PROFILE),"
fi
echo "=> Profile not found. Tried ${TRIED_PROFILE-} ~/.bashrc, ~/.bash_profile, ~/.zshrc, and ~/.profile."

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When TRIED_PROFILE is empty, this will be Tried ~/.bashrc - note the double space. iow, I think the space should go on line 335, and here, there should be no space after ${TRIED_PROFILE-}.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmmmm ... Just thought about the code readability so I did this, I can tweak it.

@ljharb
ljharb merged commit 62fb0d0 into nvm-sh:master Oct 4, 2017
@PeterDaveHello
PeterDaveHello deleted the fix-install.sh branch October 4, 2017 08:46
edwmurph pushed a commit to edwmurph/nvm that referenced this pull request Apr 9, 2018
[Fix] install.sh shouldn't use not existed variable
ljharb added a commit that referenced this pull request Mar 24, 2026
…profile detection

- Use `=` instead of `==` for string comparison (POSIX compliance)
- Use `printf '%b'` instead of variable as format string (prevents `%` characters in paths from being interpreted as format specifiers)
- Fix `TRIED_PROFILE` to reference `PROFILE` instead of `NVM_PROFILE` which is known to be empty at that point

Bugs introduced in a24ff3e, b6f1c15, and a461a0f (PR #1605).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

installing nvm Problems installing nvm itself

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants