Fallback to relative path if directory doesn't exist.

This commit is contained in:
Sergio Benitez 2016-10-03 17:25:55 -07:00
parent 647efe15d1
commit 9cf23ae2d4
1 changed files with 9 additions and 4 deletions

View File

@ -5,11 +5,16 @@ SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
function relative() {
local full_path="${SCRIPT_DIR}/../${1}"
# Use readlink as a fallback to readpath for cross-platform compat.
if ! command -v realpath >/dev/null 2>&1; then
echo $(readlink -f "${full_path}")
if [ -d "${full_path}" ]; then
# Use readlink as a fallback to readpath for cross-platform compat.
if ! command -v realpath >/dev/null 2>&1; then
echo $(readlink -f "${full_path}")
else
echo $(realpath "${full_path}")
fi
else
echo $(realpath "${full_path}")
# when the directory doesn't exist, fallback to this.
echo "${full_path}"
fi
}