Skip to content
Snippets Groups Projects
Brandon Rodriguez's avatar
db476b91
Name Last commit Last update
documents
tests
.editorconfig
readme.md
utils.sh

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 import, this script automatically changes the terminal to the location of the script's directory. This is to keep relative path handling the uniform, regardless of original calling location.

Terminal location resets on script end.

Passed Args

On import, this script splits passed args into "args", "kwargs", and "flags", via the handle_args_kwargs() function (see below for details).

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".

Script Functions

See code for more detailed documentation.

Script Setup Functions

These execute automatically on script import.

  • normalize_terminal - Normalizes terminal location to same directory as utils script.
    • Makes it so that relative directories are handled the same, regardless of what directory user is in when starting a script.
  • handle_args_kwargs - Parses all passed args into 3 sets:
    • args - Any values that do not match the other two below types.
      • Ex: True, 5, and test.
    • Kwargs - Key-value pairs, where the key comes first and starts with "--" or "-".
      • Note that the associated value must come immediately after the key.
      • Ex: -type int, --dir ./test, and --name Bob.
    • flags - Any values that start with "-" or "--", and must be defined by the calling script.
      • To define flags, populate a possible_flags array with the flag values prior to calling function or util script.
      • Interpreted as a boolean. True if present in flags array, False if not.
      • Ex: -a, --b, -run, and --test.

Directory Functions

  • get_absolute_path - Gets absolute path of passed location.
  • get_directory_name - Gets directory name of current terminal location or passed location.
  • parse_file_name - Determines file_name and file_extension of passed file location.

User Functions

  • check_is_user - Checks if executing user matches passed value.
  • check_is_not_user - Checks if executing user does not match passed value.

String Functions

  • to_upper - Converts passed value to be fully uppercase.
  • to_lower - Converts passed value to be fully lowercase.

Other

  • get_user_confirmation - Prompts user for a yes/no response. Returns True if yes.
  • display_text_colors - Displays all text color values provided by script.
  • display_args_kwargs - Displays all flags, args, and kwargs, as determined by handle_args_kwargs().

References

See documents/references.md.