Bash - Utility Script
Description
A helper/utility script to be imported by other scripts.
Intended to hold very common functionality (that's surprisingly not built into bash) such as string upper/lower, and checking current user value, and prompts of yes/no user input.
Using the Script
First, copy the utils.sh
script into your desired project folder.
Next, call it in a different script with the line . $(dirname ${0})/<relative_path_to_script_file>
. Note that this
should be near the top of the file.
For example, if both utils.sh
and the calling script are in the same directory, then the syntax would be:
. $(dirname ${0})/utils.sh
At this point, you should have full functionality of the utils.sh
file.
Terminal Current Directory
On importing this script, it will automatically change the terminal to the location of the script's directory. This is to keep relative path handling the same, regardless of calling location.
Terminal location resets on script end.
Passed Args
On import, this script splits passed args into "args", "kwargs", and "flags":
- Flags are any variable that starts with "-".
- These are effectively treated as booleans. True if present, false otherwise.
- Ex: "-a", "-b", and "-run".
- Kwargs are key-value pairs, where the key comes first and starts with "--".
- Note that the associated value must come immediately after the key.
- Ex: "--type false", "--dir ./test", and "--name Bob".
- Args are any other values that don't match above formats.
Note that this logic can be called within functions as well.
Due to how bash works, this will override the original passed script values. These original values will be backed up to
variables called "global_args", "global_kwargs", "global_flags".
References
See documents/references.md
.