In software projects, often would appear a 'scripts' directory with various
scripting utilities. The unexperienced
bash script writer would usually
litter up the directory with many execution entry points such as:
build.sh run.sh deploy.sh
If we would like to clean this up and combines these functions to a single
command.sh, how one would do so?
A nice way to accomplish this is the following:
#!/bin/bash # ... common code here ...
(for clarification regarding
"$@", see the previous post regarding execution relay)
I call this method the Functional Command Relay pattern, as it allows execution of each function with the parameters to these functions relayed as-is.
$ ./command.sh build <params> <output of the original build logic>
We can choose how strict we are with the first parameter. For example, by replacing
"$@" line with the following:
Worth to mention that the under-utilized function concept in
bash is more
powerful that it may seem at first.
The functions behave like mini-scripts in the same manner that original scripts
would behave, and similar to "real" executable programs that are already
$PATH. For instance, it is possible to use
echo for output
and have use functions as components in shell pipes.