Add absolute path helpers

This commit is contained in:
2015-07-03 13:03:28 +01:00
parent 2560964db6
commit 246d9bac5e
2 changed files with 31 additions and 0 deletions

View File

@@ -31,6 +31,7 @@ shopt -s extglob
# Helper functions
#
source "../lib/helpers/absolute-paths.sh"
source "../lib/helpers/arguments.sh"
source "../lib/helpers/trim.sh"

View File

@@ -0,0 +1,30 @@
# Get absolute path to directory given file resides in.
#
# Example:
#
# $ cd /tmp/foo-bar
# $ abs_dirname hello/world.txt
# /tmp/foo-bar/hello/world.txt
#
abs_dirname() {
local cwd="$(pwd)"
local path="$1"
while [ -n "$path" ]; do
cd "${path%/*}"
local name="${path##*/}"
path="$(resolve_link "$name" || true)"
done
pwd
cd "$cwd"
}
abs_path() {
local path="$1"
echo "$(cd "$(abs_dirname "$path")" && pwd)/$(basename "$path")"
}
resolve_link() {
$(type -p greadlink readlink | head -1) $1
}