From 9f2a8e388b94403a19f5d2bb99f5f605ab2e5435 Mon Sep 17 00:00:00 2001
From: Brandon Rodriguez <brodriguez8774@gmail.com>
Date: Sat, 10 Oct 2020 09:25:02 -0400
Subject: [PATCH] Create abs_path function

---
 documents/references.md |  3 +++
 utils.sh                | 52 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 55 insertions(+)

diff --git a/documents/references.md b/documents/references.md
index a779f9a..1cbca98 100644
--- a/documents/references.md
+++ b/documents/references.md
@@ -39,6 +39,9 @@ Various references used in project.
 ### Check for Root User
 <https://stackoverflow.com/a/18216122>
 
+### Get Absolute Directory
+<https://stackoverflow.com/a/21188136>
+
 ### Text Manipulation
 #### Change Output Color
 <https://stackoverflow.com/a/5947802>
diff --git a/utils.sh b/utils.sh
index 0e13cc5..2bee114 100755
--- a/utils.sh
+++ b/utils.sh
@@ -27,6 +27,58 @@ return_value=""
 #endregion Global Utility Variables
 
 
+#region Directory Functions
+
+###
+ # Gets absolute path of passed location.
+ ##
+function get_absolute_path () {
+
+    # Check number of passed function args.
+    if [[ ${#} == 1 ]]
+    then
+        # Expected number of args passed. Continue.
+
+        # Check location type.
+        if [[ -f ${1} ]]
+        then
+            # Handle for file.
+            return_value="$(cd "$(dirname "$1")"; pwd -P)/$(basename "$1")"
+
+        elif  [[ -d ${1} ]]
+        then
+            # Handle for directory.
+
+            # Extra logic to properly handle values of "./" and "../".
+            local current_dir="$(pwd)"
+            cd ${1}
+
+            # Then call this to have consistent symlink handling as files.
+            return_value="$(cd "$(dirname "$(pwd)")"; pwd -P)/$(basename "$(pwd)")"
+
+            # Change back to original location once value is set.
+            cd ${current_dir}
+        else
+            echo -e "${text_red}Passed value ( ${1} ) is not a valid file or directory.${text_reset}"
+            exit 1
+        fi
+
+    # Handle for too many args.
+    elif [[ ${#} > 1 ]]
+    then
+        echo -e "${text_red}Too many args passed. Expected one arg, received ${#}.${text_reset}"
+        exit 1
+
+    # Handle for too few args.
+    else
+        echo -e "${text_red}Too few args passed. Expected one arg, received 0.${text_reset}"
+        exit 1
+    fi
+}
+
+#endregion Directory Functions
+
+
 #region User Check Functions
 
 ###
-- 
GitLab