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

How to keep ShellCheck happy ? #8

Closed
ngirard opened this issue Nov 4, 2020 · 2 comments
Closed

How to keep ShellCheck happy ? #8

ngirard opened this issue Nov 4, 2020 · 2 comments

Comments

@ngirard
Copy link

ngirard commented Nov 4, 2020

Running ShellCheck against a "slapped" script produces one warning per variable ; for instance:

)"; [[ -z "${_success}" ]] && exit 1
           ^---------^ SC2154: _success is referenced but not assigned.

How could we silent ShellCheck when using Slap ?
The only way I could think of is to add

# shellcheck disable=SC2154

below the shebang ; but doing so will prevent further errors with non-slap variables from being reported.

Any thoughts ?

@ngirard ngirard changed the title How to keep SpellCheck happy ? How to keep ShellCheck happy ? Nov 4, 2020
@guardam
Copy link
Owner

guardam commented Nov 6, 2020

https://github.com/koalaman/shellcheck/wiki/SC2154#exceptions

Seems like the viable workarounds are:

  • Using ${var:?}. This means that if var is unset or empty, the program will exit with an error code.
  • Explicitly declare the variables before or after the eval, with declare var, or initialize the variables before the eval with var="".
  • Initialize if not assigned, for example ${var:=}.
  • Use shellcheck disable=SC2154 before the problematic line. If you put this directive after the shebang it will be applied to all the file, if you put it just before the line, it will be applied only to that line.

Sadly this problem can't be solved from the "slap side". The only possible workaround I can think of is to export an hash-map instead of multiple variables, in which case you would need only one directive (for the hash-map), and not for its values. This is possible to implement for shells like BASH and ZSH, but some shells don't support this, most notably the POSIX sh standard.

I would like to have the option to export as an hash-map for shells that support hash-maps, so in the future it will be added. For now the user has to choose from one of the four workarounds before-mentioned. I'm going to add a section to the README, thank you.

@ngirard
Copy link
Author

ngirard commented Nov 6, 2020

Thanks for your thoughts !

* Use `shellcheck disable=SC2154` before the problematic line. If you put this directive after the shebang it will be applied to all the file, if you put it just before the line, it will be applied only to that line.

Oh, I didn't know that was possible. Putting the directive right after the slap invocation seems like a good compromise to me !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants