Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This commit demonstrates an example project structure to introduce subcommands and simple argument parsing. The goal is to increase support for end-user environments by using a wrapper script which will always execute in a POSIX-compatible shell environment.
It is worth noting, however, that this technique may be better applied by calling
bash
instead ofsh
. Bash has, in practical terms, nearly equal coverage as POSIX shell. The only caveat is really MacOS, which only ships the final 3.x release. No problem though, because bash3 has plenty of support for nicer features like arrays. The project already has a hard dependency ontmux
anyway, right? And the target audience is also expected to be running a modern Linux (arch btw?) or MacOS, if I have the correct read on the community here. What is the worst case? PerhapsWSL
?Git Bash
on Windows? Surely not, I suspect, an environment likecygwin
ormingw
, but I pass no judgment. For your consideration.If anything more complex was necessary, this would cease to be worth doing in pure shell script anymore, except as academic exercise (an admirable goal, nevertheless). In such case, I would recommend bumping up to bash4 instead, since the purpose of the script would most likely no longer have end-users in mind and may make more guarantees that bash4+ will be available in your environment (e.g. part of your custom build system).
getopts
is also another POSIX alternative to parse$@
. Unfortunately, it only supports a very narrow use-case; namely only short single letter options following a single dash e.g.-h
forhelp
. Worth noting anyway, as it may be additionally employed alongside the provided implementation in this changeset. Subcommands may desire additional option switches, for example.Test this script:
Further reading:
These were written by wizards, for mortals like me.
getopts
TutorialEDIT: I forgot to mention that I also turned off the unnecessary executable bit on all the
*.sh
files.