Yesterday's meeting was a fun
deep-dive into some `bash` quirks.
Mostly on how to determine if a
script was sourced or run as an executable.
The reason being the use of `exit` statements as "gate clauses" to
detect error conditions and abort instead of nesting the desired
code inside if...elif...else...fi statements.
In a sourced script, `exit` will
close the user's terminal.
We poured over a technique I
found on StackOverflow years ago that checks for an error on
calling `return` in an executed script (it's illegal there) and
quits the program if the error does not occur.
Requires masking error messages generated when script is
executable, running `set +e`, restoring the status of `set -e`,
and other nonsense.
Hugh suggested that scripts
should never be sourced unless specific conditions are desired.
Not wrong, but the desire is to prevent users' terminal from
closing on them, forcing the correct invocation.
Lennart came up with a clever
technique, and even improved upon it by embedding it in a
function.
I went with that method, then
modified further to simply test for $0
== -bash, which is always true if the script was
sourced.
Thanks everyone that joined the
meeting and especially Lennart for his contribution.
Also, some clever shell tricks
shared on the KWLUG mailing list recently, highly worth a quick
read:
> Not sure who could benefit from this, but here's a
site with some useful shell tricks:
>