The power is out on your boat, again. It’s 3am. You suspect that, again, the alternator housing has come loose.
You duct tape a flashlight to the bulkhead so you can work hands free and actually see what you are doing. All you have on you is a broken pocket knife but it’ll do because all you need to accomplish right now is to tighten the housing screws enough. You know this for a fact because you’ve done it three times already in the last 24 hours.
It’s not even a documented procedure — you’ll replace the housing mounts entirely when you’re back at port in three days’ time. You guarantee it — this is the first thing you’ll do even, when you get back to shore. You have my word on that, captain!
The duct tape came unstuck. It was damp and doesn’t work so well (at all) when it’s wet. The flashlight survived the fall. More tape this time should do the job. Tape mount version 2 will still unstick of course, eventually. Nothing stops the damp at sea, but if you use enough tape then you’ll have fixed the power by the time the tape fails. That’s your plan B and you’re sticking to it.
Sure, you could do this job better if you had an impact driver with an automatically illuminated bit chuck, but buying one of those is further down the todo list than fixing the power on the boat, making it back to port, and ensuring the power doesn’t fail this way again, as promised. Or at least won’t fail for the next few shifts.
On your days off you relax by programming in Bash.
help() {
cat <<'EOH'
my-script — does one thing well
Usage:
my-script <input> <output>
Options:
<input> Input file to read.
<output> Output file to write. Use '-' for stdout.
-h Show this message.
EOH
}
If the indentation bugs you, you can use a simpler sed trick to remove leading space so that you can indent it as desired:
help() {
sed -e 's/ //' <<'EOH'
my-script — does one thing well
Usage:
my-script <input> <output>
Options:
<input> Input file to read.
<output> Output file to write. Use '-' for stdout.
-h Show this message.
EOH
}
#!/bin/bash
USAGE="my-script — does one thing well
Usage:
my-script <input> <output>
Options:
<input> Input file to read.
<output> Output file to write. Use '-' for stdout.
-h Show this message.
"
help() {
echo "$USAGE"
}
This is my standard approach which is cleaner for putting the documentation at the very top of the file like the linked article.
As far as I know. The POSIX spec[1] simply declares that in single quotes all characters will be preserved exactly, except for single quotes, which aren't allowed. Likewise, for double quotes, except that it also performs expansions ($) and allows escaping (\).
That's exactly what I do. For others who were not aware that multi-line strings can be used, this is POSIX-compatible (most of my shell scripts are executed by `dash`).
I've always avoided that for fear that my or someone else's editor will accidentally replace the tabs with space. Mixed is not a common configuration, these days.
a commonly used feature that has been a part of the unix standard since before "bash" only meant to hit something with a heavy object, while linus was getting his diaper changed is not an "obscure feature."
a five year old took his first aware car ride, and at a gas station saw the trunk of the car next to theirs open. he said "seriously, bmw has too many obscure features." after all, a car for him was where you put the baby seat. and why would you put that in a compartment with no windows or air, that's too small to even fit a baby seat.
I believe the point of the article's method is that it allows you to document your script with code comments, and then reuse the same text for help output
The neat thing about not indenting it is that you can make use of your editor's text-width auto-wrapping. For example, if you have it set to 80 columns, indenting it would make the text-width of the help text 76 in your case.
Also, putting the help text in code like this instead of a comment allows one to expand $0 so that the command name in the help text always matches the filename and path used in the invocation.
So I took some of the advice and tips offered in there, and wrote a template file to be used as a baseline when writing scripts for any project that might need one:
EDIT: -for anyone who would like to read some actual examples- I have to manage a bunch of scripts so actually a slightly more up to date version of the template is put into practice by means of a common bash.conf file that then gets sourced by all scripts: https://github.com/Kurento/adm-scripts/blob/master/bash.conf...
You're welcome! Shell scripting has a weird language, unsafe by default, and very prone to mistakes... but knowing it well pays off.
People say that for complex things it's better to write Python, but that doesn't fly in embedded or Docker environments. Python is not even present in the default Ubuntu Docker images. Also if all you want to do is really write glue code between CLI programs, shell scripting is the way to go.
A bash pitfall which I have experienced but didn’t see mentioned is the behavior of the `set -e` (errexit) option when using command substitution. If you expect failures within the command substitution to cause the script to exit, you’re gonna be confused.
Thanks. I wasn't aware of that option. I've added the following to my Bash-specific shell scripts:
# Cause command substitution to inherit the value of the `errexit` option.
# Introduced in Bash 4.4
if [ "${BASH_VERSINFO[0]}" -gt 4 ] ||
{ [ "${BASH_VERSINFO[0]}" -eq 4 ] && [ "${BASH_VERSINFO[1]}" -ge 4 ]; }; then
shopt -s inherit_errexit
fi
That wouldn't work in the general case. Those patterns would also match long options. If I add a case pattern `--all)`, and I call the script with `--all`, it's also going to match
-*a*|--arg)
You could fix that with:
-a*|-[!-]*a*|--arg)
> It doesn't support args of the form -avalue but those a pretty uncommon anyway.
You could
-a*|-[!-]*a*|--arg)
if [[ "$1" != --arg ]]; then
value="${1#*a}"
fi
if [[ ! "$value" ]]; then
value="$2"
shift
fi
;;&
Putting the option stuck together to its value has the advantage of working nicely with brace expansion. For example, you can call `strace -p{1111,2222,3333}` to trace those 3 pids and avoid having to type `-p` 3 times.
There's still one problem. To exemplify it, if you call with `-av`, it'll process the `v` as the option `-v` instead of the option value to `-a`. If you only have one possible option that takes a value, this can be fixed by putting its case clause before all others. If you have more, then that'll require things to get a little more complicated:
-d*|-[!-]*d*|--debug)
if [[ ! "$finished_case" && ("$1" = --debug || "$1" =~ '^[^ad]*d') ]]; then
debug=true
fi
;;&
-a*|-[!-]*a*|--arg)
if [[ ! "$finished_case" && ("$1" = --arg || "$1" =~ '^[^a]*a') ]]; then
if [[ "$1" != --arg ]]; then
value="${1#*a}"
fi
if [[ ! "$value" ]]; then
value="$2"
shift
fi
finished_case=true
fi
;;&
...
esac
shift
finished_case=
done
All case-clauses would need to use `;;&` by the way, including `-v` and `-h`. The regex is generally:
Another problem is that option and argument non-recognition would not work as previously layed out. You can include short options that aren't recognized, and they'll be ignored instead of raising errors. For positional arguments, one would need a condition to check for options, since using `;;&` for everything means that everything would land to
*)
Maybe those are the last issues, but this is already out of hand for otherwise small and simple shell scripts. All these complications arise from trying to support the sticking together of short options and their possible values. Processing arguments in a case loop is much, much simpler if we avoid supporting those 2 features.
I suppose it depends on the use case. Personally I've always thought argparse is good enough, and have never hit a roadblock "because I'm using argparse" so to say. Having said that, I do like the pattern click is going for. If it argparse allowed the same pattern, in my opinion that would be cool, and it would probably be my first choice.
I didn’t mean to suggest we should reach for Click for simple help/manpage display.
The case I used it for was much more complex. What I liked about it was the easy to use API, clear documentation & examples, and readable patterns.
For simple text display, I like the solution from the article, and I learned something new about bash scripts. Also, I learned from comments you can use heredoc in bash!
A great option when you're stuck with an old crusty script that you don't want to completely rewrite in python, but do want to clean up enough so that you can call it with `-h` and remember how to use it a few months in the future.
Unfortunately, this won't help you if you're on embedded where python isn't in the base system.
PowerShell also has comment-based help, which is like a manpage embedded as a comment within the script. It's like OP's suggested help format, but better.
I also really like the "flag" package for Go, it generates the help text for you and easily lets you set defaults as well as helps you type-check inputs.
My rule for a long time has been that any time you have more than one screen's worth of code and/or are using any of arrays, functions, or more than simple positional arguments you'll save time by rewriting it in Python. shellcheck has helped soften that a bit but the usability difference is pretty noticeable even with multiple decades writing shell scripts.
This seems like a neat sed trick, but I'm not sure that it's useful for this particular case?
When I write a shell script, I often write a help function if it's not a totally trivial script, but there's no need for this cryptic sed expression, right? You can just call `echo` a few times and do it the obvious way. That works better for maintainability and if you put it at the top of the file then it's immediately visible when opening or using `head` on it.
Neat trick though - sed is super useful for all kinds of things. I had a co-worker who bound a keybinding to a sed one-liner that would automagically reconfigure some files that needed to be changed regularly. I ended up using that trick to allow for changing my terminal console colorscheme with a single command.
It's a little sad that standard shell here documents only support elliding leading tabs (it wouldn't be so sad if the record separator hadn't been thrown under the bus - having a character for indentation distinct from space is good... In theory).
But at any rate my typical usage() is generally along these lines (warning watch out for expansions):
I think it'd be better without using basename, just $0. That way it matches the way it was called, which is how the user chose to access it for whatever reason. The bare filename might refer to a different command, even. Also, if you include examples in the help text, they'll also be able to copy and paste, instead of having to manually insert what basename stripped away.
True enough - I find it depends a bit on the nature of the script - if it's something buried under misc/tools/extra/bin/util.sh - i tend to prefer brevity - especially in the first paragraph/usage section (util.sh <required param> [optional param]).
But for more concrete examples I'll often leave off the basename - for easier cut and paste.
Right, it's a bit useless trick. I guess the author is just exploring how to organize help, maybe thinking about larger scripts or maybe doing it for the article. Either way it's hard to see it going anywhere with sed. If you were to explore parsing and organizing help, I'd suggest starting with a simple pure shell loop like this:
while read -r line; do
case "$line" in
"###"*)
echo "${line#\###}" ;;
esac
done <"$0"
I also posted to the github gist, this the sed command here is not cross-plataform friendly. You can accomplish the same thing with an awk command though:
This reminds me of a nice sed one-liner I recently happened to craft.
Do you ever collect families of functions in your shell scripts under different sections? Here's a nice way of printing out all the functions under a given section:
funs(){ sed -n '/^## /h;x;/'"$1"'/{x;s/^\(\w\+\)().*/\1/p;x};x' "$0";}
Where "sections" are delimited by comments of the form "## Section Name" at the beginning of a line. A particularly nice use case is when you write scripts that expect "subcommand" arguments, like
$ foo.sh bar baz
and wish to keep track of the available subcommands in the help documentation. Simply collect all your subcommands under the heading "## Subcommands" and stick a funs call in your documentation:
The sed one-liner above uses the oft-ignored "hold space" which lets you store data that persists between lines. Here's the same sed but expanded with comments:
funs(){ sed -n '/^## /h # Store header line in hold space
x # Swap out current line with header in hold space.
/'"$1"'/{ # Run block if last encountered header matches $1
x # Return to processing current line (instead of header)
s/^\(\w\+\)().*/\1/p # Print function names
x # Whether or not this block runs, we want to return to
# processing the current line. If the block does not
# run, then the hold space contains our current line
# with the active line being our header. So we must
} # return to that state as whell when the block is run.
x # Restore current line from hold space' "$0"
}
I'm particular about:
If I run a command and its arguments is wrong, it should output error and help messages to STDERR. But if I run a command with --help argument, it should output help messages to STDOUT.
I like the idea of combining the header with the help documentation to reduce the number of areas to maintain in smaller scripts. For larger scripts though, I think I'd still prefer to have a separate function, so that the help documentation doesn't overwhelm the initial viewing of the actual code.
I also like to feed a heredoc directly into man, which allows you to achieve nicer formatting for the help documentation. Something like this...
man -l - << EOF
.\" Manpage for encpass.sh.
.\" Email contact@plyint.com to correct errors or typos.
.TH man 8 "06 March 2020" "1.0" "encpass.sh man page"
.SH NAME
encpass.sh \- Use encrypted passwords in shell scripts
...
EOF
No, I think the readability of the help documentation itself is largely the same whether it is placed in the header or in a dedicated function. When I'm viewing the shell script code though, often I want to jump right in and see the actual code, not look at the help documentation.
By having the help documentation in a function in the middle or towards the end of the file, I don't have to page down through the help documentation to get to the code that is actually doing things. If I'm really interested in the help documentation, then I'd prefer to look at the nicely formatted version output by the script (<script> --help or whatever) rather than looking in the actual script code anyway.
Admittedly, this may be more of a subjective personal preference item.
Even better: Don’t use bash. I started using Python instead of bash. It’s way better to read and more maintainable. If I need the performance of native-Unix commands, I can still use them using subprocess.
Python is not as handy/efficient as bash when you want to utilize some existing Unix commands. And normally it’s crucial to stop bleeding fast if something bad is happened to you server/cluster
I've been migrating shell scripts to python and have found the 'sh' library invaluable for pulling in pure CLI commands from the bash script and adding to the python implementation. http://amoffat.github.io/sh/
It's essentially an abstraction layer above subprocess. Quick example - to use ifconfig natively in python:
Python is surprisingly bad at managing subprocesses, though. Read the subprocess docs closely, it's quite easy to get deadlocks if you try to compose operations the way you can in bash.
I used this strategy in "fx" which is a development helper frontend for fuchsia builds and tools. I used four # for the "short description" and three for the long description. The reason I used the strategy there is that lot of our scripts delegate arguments to native commands, and so adding help purely to --help wasn't really a good ROI. Implementation: https://fuchsia.googlesource.com/fuchsia/+/refs/heads/master...
Something I've wanted to do for a while is build a library to parse and generate use lines / documentation that adheres to the posix useline spec (can't find the link at the moment) while also being able to idempotently (de)serialize and be descriptive enough to define arguments and flags in a way a human could easily understand. iirc the spec seemed probably too vague to just work with all the currently existing man pages, but it would be nice to have a spec all programs can follow that machines can parse on my os.
My time to shine! I built an argument parser that uses a POSIX compliant help message as the input. It's a parser generator really.
It generates minified bash that is inlined in your script, so no dependencies. The work is based off of docopt (and is docopt compliant).
Check it out: https://github.com/andsens/docopt.sh
I wouldn't know, but there is no reason for me to be thinking something like that. "Samizdat" is not really a name or something, it's a transliteration of "самиздат", which is a short/colloquial for "самостоятельное издательство", which literally means "self-publishing" (this was a thing during the USSR, where "self-publishing" was basically opposed to "real, official government-approved publishing"). I believe it's just a "clever" domain somebody was able to acquire, nothing more.
I just spent three days explaining a bash script I authored to a tech lead who "doesn't know bash that well". If I had written in more help messages I probably could have preserved more of my time.
There's an endless variation on how shell scripts can present help information. Here's another, consider this array:
ARGUMENTS+=(
"a,arch,Target operating system architecture (amd64)"
"b,build,Suppress building application"
"o,os,Target operating system (linux, windows, mac)"
"u,update,Java update version number (${ARG_JRE_UPDATE})"
"v,version,Full Java version (${ARG_JRE_VERSION})"
)
The lines are machine-readable and alignment is computed by the template:
When install script[0] help is requested, the following is produced:
$ ./installer -h
Usage: installer [OPTIONS...]
-V, --verbose Log messages while processing
-h, --help Show this help message then exit
-a, --arch Target operating system architecture (amd64)
-b, --build Suppress building application
-o, --os Target operating system (linux, windows, mac)
-u, --update Java update version number (8)
-v, --version Full Java version (14.0.1)
Using an array reduces some duplication, though more can be eliminated. Scripts typically have two places where the arguments are referenced: help and switch statements. The switch statements resemble:
Usually parsing arguments entails either assigning a variable or (not) performing an action later. Introducing another convention would allow hoisting the switch statement out of the installer script and into the template. Off the cuff, this could resemble:
ARGUMENTS+=(
"ARG_ARCH,a,arch,Target operating system architecture (amd64)"
"do_build=noop,b,build,Suppress building application"
"ARG_JRE_OS,o,os,Target operating system (linux, windows, mac)"
"ARG_JRE_UPDATE,u,update,Java update version number (${ARG_JRE_UPDATE})"
"ARG_JRE_VERSION,v,version,Full Java version (${ARG_JRE_VERSION})"
)
The instructions to execute when arguments are parsed are thus associated with the arguments themselves, in a quasi-FP style. This approach, not including the FP convention, is discussed at length in my Typesetting Markdown series[1].
You duct tape a flashlight to the bulkhead so you can work hands free and actually see what you are doing. All you have on you is a broken pocket knife but it’ll do because all you need to accomplish right now is to tighten the housing screws enough. You know this for a fact because you’ve done it three times already in the last 24 hours.
It’s not even a documented procedure — you’ll replace the housing mounts entirely when you’re back at port in three days’ time. You guarantee it — this is the first thing you’ll do even, when you get back to shore. You have my word on that, captain!
The duct tape came unstuck. It was damp and doesn’t work so well (at all) when it’s wet. The flashlight survived the fall. More tape this time should do the job. Tape mount version 2 will still unstick of course, eventually. Nothing stops the damp at sea, but if you use enough tape then you’ll have fixed the power by the time the tape fails. That’s your plan B and you’re sticking to it.
Sure, you could do this job better if you had an impact driver with an automatically illuminated bit chuck, but buying one of those is further down the todo list than fixing the power on the boat, making it back to port, and ensuring the power doesn’t fail this way again, as promised. Or at least won’t fail for the next few shifts.
On your days off you relax by programming in Bash.