Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

become a safer shell script #868

Closed
herzi opened this issue Oct 12, 2015 · 15 comments
Closed

become a safer shell script #868

herzi opened this issue Oct 12, 2015 · 15 comments
Assignees
Labels
bugs Oh no, something's broken :-(

Comments

@herzi
Copy link

herzi commented Oct 12, 2015

In our CI setup, we prefix shell scripts with

set -o errexit # exit on first error
set -o nounset # unset variables are errors

in order to make sure script errors will become more obvious. However, I can only enable these lines after including nvm.shdue to this error:

/usr/local/opt/nvm/nvm.sh: line 1918: $1: unbound variable

Please consider adding the aforementioned shell settings in your code (and to fix unbound variable access).

Debug Info

bash-3.2$ nvm --version
0.26.1
@ljharb
Copy link
Member

ljharb commented Oct 12, 2015

Using nonzero exit codes for flow control is a normal part of programming, and nvm is not compatible with set -e (the errexit option) - see #865 (comment).

In addition, v0.26.1 is out of date - please try installing v0.29.0. I'm happy to look at any variable errors you see on the latest version, but errexit interferes with normal operation of nvm. (Relates to #865, #866, #721).

Note that this particular nounset error is complaining about $1 which is being used as an argument to the "source nvm" command in your profile file - the whole point is that it's unset, and I'm testing that it's unset, so that I can conditionally install-on-source.

In other words, these shell options are a crutch that both interfere with normal operation as well as enable developers to not properly check all possible failure conditions.

@ljharb ljharb closed this as completed Oct 12, 2015
@ljharb ljharb added the needs followup We need some info or action from whoever filed this issue/PR. label Oct 12, 2015
@herzi
Copy link
Author

herzi commented Oct 12, 2015

About errexit: Note that this does not apply to any invocation that is either:

  • the argument deciding which branch to take for an if, or
  • code that has error handling capabilities like || <your error handler here>

WRT nonuset: You can safely check the value of $# instead of just referencing into the arguments array.

@ljharb
Copy link
Member

ljharb commented Oct 12, 2015

That's a fair point - I'll be happy to fix that variable check by checking $# prior to $1.

If you can point at specific instances where errexit breaks, and a to a place where nvm can fix it without impeding code clarity, I'm all for it!

ljharb added a commit to ljharb/nvm that referenced this issue Oct 13, 2015
@ljharb
Copy link
Member

ljharb commented Oct 13, 2015

I've taken care of the nounset error. Let me know if you see any others.

@ljharb ljharb added the pull request wanted This is a great way to contribute! Help us out :-D label Oct 13, 2015
@ljharb ljharb reopened this Oct 13, 2015
@ljharb
Copy link
Member

ljharb commented Oct 13, 2015

Reopening this in case someone wants to attempt to identify and fix these issues.

@ljharb ljharb closed this as completed in 0b9526e Oct 15, 2015
@ljharb ljharb reopened this Oct 15, 2015
@danistefanovic
Copy link

I've found the following unbound variables so far:

  • line 77: NVM_DIR: unbound variable
  • line 1655: PROVIDED_REINSTALL_PACKAGES_FROM: unbound variable
  • line 1658: PROVIDED_REINSTALL_PACKAGES_FROM: unbound variable
  • line 662: NVM_ADD_SYSTEM: unbound variable

NVM version: v0.30.1

@ljharb
Copy link
Member

ljharb commented Jan 13, 2016

NVM_DIR is explicitly intended to be one, but the others aren't - thanks!

@ljharb
Copy link
Member

ljharb commented Jan 13, 2016

@danistefanovic I see how PROVIDED_REINSTALL_PACKAGES_FROM is being checked in one case even when it's empty, but I explicitly set $NVM_ADD_SYSTEM to false on line 603 - can you elaborate on the problem with that one? Both are declared with local so I'm also not clear on what you mean by "unbound"

@danistefanovic
Copy link

"Unbound variable" is the error message in the terminal if you enable the nounset option.

set -o nounset
. ./nvm/nvm.sh
./nvm/nvm.sh: line 77: NVM_DIR: unbound variable

I understand that NVM_DIR should be provided from "outside" to set a custom NVM directory. For this case, you can use parameter expansion for the check:

if [ -z "${NVM_DIR-}" ]; then

# instead of
# if [ -z "$NVM_DIR" ]; then

This seems to work with the nounset option. I've tested it in bash (Ubuntu) and zsh (OS X).

@ljharb
Copy link
Member

ljharb commented Jan 17, 2016

Thanks, this has actually helped me find a ton of these already. I'm going to do a full pass, and I'll close this issue with that commit.

@ljharb ljharb added bugs Oh no, something's broken :-( and removed needs followup We need some info or action from whoever filed this issue/PR. pull request wanted This is a great way to contribute! Help us out :-D labels Jan 17, 2016
@ljharb ljharb self-assigned this Jan 17, 2016
@ljharb ljharb closed this as completed in 00a8b36 Jan 17, 2016
@danistefanovic
Copy link

Glad I could help 🍻

@stouf
Copy link

stouf commented Mar 18, 2016

Hi there !

Just ran into another unbound variable issue :(

nvm/nvm.sh: line 95: NVM_IOJS_ORG_MIRROR: unbound variable

This error occurs when I have the set -u option in a bash script.

Version of NVM: 0.31.0

@ljharb
Copy link
Member

ljharb commented Mar 18, 2016

Thanks, will fix.

@stouf
Copy link

stouf commented Mar 18, 2016

@ljharb Awesome. I forked the repo and was about to send a Pull Request, but I guess it's better to leave that to you :)

@ljharb
Copy link
Member

ljharb commented Mar 18, 2016

@stouf thanks! PRs are always welcome but this is small enough that it's nbd ¯_(ツ)_/¯

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bugs Oh no, something's broken :-(
Projects
None yet
Development

No branches or pull requests

4 participants