diff --git a/documents/references.md b/documents/references.md index eab90c651cee6962c66e98bcec42164aefe4a1ad..fcb09190ddfb9abea1504bd3d34b773fc63236ec 100644 --- a/documents/references.md +++ b/documents/references.md @@ -34,6 +34,7 @@ Various references used in project. #### Getting Directory of Calling Script <https://stackoverflow.com/a/59916> +<https://stackoverflow.com/a/246128> #### Get Absolute Directory <https://stackoverflow.com/a/21188136> diff --git a/readme.md b/readme.md index 235335d3c2bf052cf443c06da0869c4967146d26..fc3ec4a1c825edabba5e69848d5e1c94ccac3057 100644 --- a/readme.md +++ b/readme.md @@ -20,6 +20,12 @@ For example, if both `utils.sh` and the calling script are in the same directory 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. + ## References See `documents/references.md`. diff --git a/utils.sh b/utils.sh index 2bee1143538c7c810bed3ab2c69aedad4d57c879..b90b3c41ca346d5f8167fefa3b8ba5422c8fcb47 100755 --- a/utils.sh +++ b/utils.sh @@ -29,6 +29,20 @@ return_value="" #region Directory Functions +### + # Normalizes the location of the terminal to directory of utils.sh file. + # + # The point is so that the execution of a script will handle the same, regardless of terminal directory location at + # script start. Ex: User can start script at either their home folder, or project root, (or any other directory) and + # relative directory handling will still function the same in either case. + # + # Automatically called on script import. + ## +function normalize_terminal () { + cd "$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" +} + + ### # Gets absolute path of passed location. ## @@ -242,3 +256,7 @@ function display_text_colors () { echo -e " ${text_white}White${text_reset}" echo "" } + + +# Functions to call on script import. +normalize_terminal