From 49a7f3ae42996eb9b6e223ccd1ddcd5d9f8110b1 Mon Sep 17 00:00:00 2001 From: Brandon Rodriguez <brodriguez8774@gmail.com> Date: Sat, 10 Oct 2020 10:29:50 -0400 Subject: [PATCH] Add normalize function --- documents/references.md | 1 + readme.md | 6 ++++++ utils.sh | 18 ++++++++++++++++++ 3 files changed, 25 insertions(+) diff --git a/documents/references.md b/documents/references.md index eab90c6..fcb0919 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 235335d..fc3ec4a 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 2bee114..b90b3c4 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 -- GitLab